ajf

Andrea Faulds

Contents

PHP RFC: intdiv()

  • Version: 0.2
  • Date: 2014-07-15 (Last updated 2014-07-16)
  • Author: Andrea Faulds, ajf@ajf.me
  • Status: Accepted (Fallback proposal; operator rejected), merged into master
  • First Published at: http://wiki.php.net/rfc/intdiv

Introduction

PHP currently lacks a way to do integer division. While (int)(3 / 2), which does a floating-point division then casts to integer, does work most of the time, it does not return the correct result for integers beyond 53 bits as floats cannot represent integers beyond that point without loss of information. Furthermore, doing a floating-point division then casting seems hackish; it is a workaround for a lack of integer division, not a proper way to do it. Integer division can be quite useful in some contexts, for example when splitting a value into rows and columns, or converting seconds to hours and minutes. Having this also makes the language more complete; we have integer remainder (the modulo operator, %) yet not integer division at present.

Proposal

A new infix operator is added:

var_dump(3 %% 2); // int(1)

It returns the integer division of the first operand by the second. If the divisor (the second operand) is zero, it throws an E_WARNING and returns FALSE, just as the division operator does. If the first operator is LONG_MIN (-PHP_INT_MAX - 1) and the second is -1, it returns zero, much like the % operator.

Assignment is also supported:

$foobar = 3;
$foobar %%= 2;
var_dump($foobar); // int(1)

Fallback proposal

The original proposal was just to add a function. Should the operator fail to get in, I instead propose a new function is added to ext/standard's math functions:

intdiv(int $numerator, int $divisor)

It returns the integer division of $numerator by $divisor. If $divisor is zero, it throws an E_WARNING and returns FALSE, just as the division operator does. If the first operator is LONG_MIN (-PHP_INT_MAX - 1) and the second is -1, it returns zero, much like the % operator.

Backward Incompatible Changes

The %% operator wouldn't conflict with anything.

The fallback proposal, intdiv, would conflict with any existing userland functions called intdiv, but that's unlikely.

Proposed PHP Version(s)

Next PHP 5.x (i.e. PHP 5.7).

Future Scope

None I can see.

Patches and Tests

A fully-working patch against master with tests has been made. The pull request is here: https://github.com/php/php-src/pull/724

The pull request for the fallback (and original proposal) of just the ext/standard function is here: https://github.com/php/php-src/pull/722

Implementation

It was merged into master here: https://github.com/php/php-src/commit/8e964fe665b6a4fbdc59639dad2efb4cd43d1c05

It will be in PHP 7. Since there is no 5.7 branch, it is doubtful it will actually be in 5.7.

PHP Manual - http://php.net/manual/en/function.intdiv.php

References

Somewhat inspired by Python's // operator, and the equivalent div operator in Pascal and Game Maker Language (a somewhat obscure C-like partially object-oriented scripting language, which probably inherited this from Pascal). The choice of the %% syntax is thanks to Bishop Bettini's suggestion on internals.

Rejected Features

None yet.

Versions

Version Changed Date
0.2 Propose %% operator instead
0.1 Created, intdiv() function

Votes

An option needs 2/3 votes to win

Merge the intdiv operator patch into master? (17.2% approved)
User Vote
aharvey No
ajf Yes
bishop Yes
brianlmoon No
bwoebi No
datibbaw No
daverandom No
davey Yes
derick No
dm No
fa No
gwynne No
indeyets No
jedibc No
jpauli No
kriscraig Yes
laruence No
levim No
mbeccati No
mfonda No
nikic No
peehaa No
pollita No
rasmus No
rdlowrey No
stas No
tyrael No
yunosh Yes
zeev No
If the operator vote fails to achieve a 2/3 majority, should the intdiv() function patch be merged into master? (100% approved)
User Vote
aharvey Yes
ajf Yes
bishop Yes
brianlmoon Yes
bwoebi Yes
datibbaw Yes
daverandom Yes
davey Yes
derick Yes
dm Yes
fa Yes
gwynne Yes
indeyets Yes
jedibc Yes
jpauli Yes
kriscraig Yes
laruence Yes
mbeccati Yes
mfonda Yes
nikic Yes
peehaa Yes
pollita Yes
rasmus Yes
rdlowrey Yes
stas Yes
tyrael Yes
yunosh Yes
zeev Yes