intdiv() View as Markdown

Implemented (PHP 7.0)
Authors
Andrea Faulds, ajf@ajf.me
Date
2014-07-15 (Last updated 2014-07-16)
Version
0.2
Source
wiki.php.net/rfc/intdiv

Votes

Merge the intdiv operator patch into master? closed
Yes
5
No
24
29 voters
aharvey No 2014-07-30 17:43
ajf Yes 2014-07-30 02:28
bishop Yes 2014-07-30 16:48
brianlmoon No 2014-08-01 20:47
bwoebi No 2014-07-30 16:13
datibbaw No 2014-07-30 04:11
daverandom No 2014-07-30 07:38
davey Yes 2014-08-02 02:01
derick No 2014-07-30 10:43
dm No 2014-08-05 22:41
fa No 2014-07-30 14:32
gwynne No 2014-07-30 07:54
indeyets No 2014-07-31 10:44
jedibc No 2014-07-30 08:05
jpauli No 2014-08-04 07:51
kriscraig Yes 2014-07-30 18:40
laruence No 2014-07-30 09:03
levim No 2014-07-30 13:30
mbeccati No 2014-07-30 06:53
mfonda No 2014-07-30 16:39
nikic No 2014-07-31 08:55
peehaa No 2014-07-31 07:16
pollita No 2014-07-30 17:53
rasmus No 2014-07-30 03:54
rdlowrey No 2014-07-30 16:14
stas No 2014-07-30 17:53
tyrael No 2014-08-03 15:33
yunosh Yes 2014-08-04 09:09
zeev No 2014-07-31 08:57
If the operator vote fails to achieve a 2/3 majority, should the intdiv() function patch be merged into master? closed
Yes
28
No
0
28 voters
aharvey Yes 2014-07-30 17:42
ajf Yes 2014-07-30 02:28
bishop Yes 2014-07-30 16:47
brianlmoon Yes 2014-08-01 20:47
bwoebi Yes 2014-07-30 16:13
datibbaw Yes 2014-08-01 03:41
daverandom Yes 2014-07-30 07:38
davey Yes 2014-08-02 02:01
derick Yes 2014-07-30 10:43
dm Yes 2014-08-05 22:41
fa Yes 2014-07-30 14:31
gwynne Yes 2014-07-30 07:54
indeyets Yes 2014-07-31 10:44
jedibc Yes 2014-07-30 08:06
jpauli Yes 2014-08-04 07:51
kriscraig Yes 2014-07-30 18:40
laruence Yes 2014-07-30 09:04
mbeccati Yes 2014-07-30 06:53
mfonda Yes 2014-07-30 16:39
nikic Yes 2014-07-31 08:55
peehaa Yes 2014-07-31 07:16
pollita Yes 2014-07-30 17:53
rasmus Yes 2014-07-30 03:54
rdlowrey Yes 2014-07-30 16:14
stas Yes 2014-07-30 17:54
tyrael Yes 2014-08-03 15:33
yunosh Yes 2014-08-04 09:10
zeev Yes 2014-07-31 08:57

Discussions on the mailing list

[RFC] intdiv()
by Andrea Faulds · 43 messages
[VOTE][RFC] intdiv()
by Andrea Faulds · 16 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

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.

Vote

Two votes are being held simultaneously:

  1. Merge the intdiv operator (% %) patch into master? Yes/No (requires 2/3 majority, language change)
  2. If the % % operator vote fails to achieve a 2/3 majority, should the intdiv() function patch be merged into master? Yes/No (requires 50%+1 majority, non-language change)

As the ext/standard function is to be considered a fallback option only, it would not be merged if the proper operator gets in.

Voting opened 2014-07-30 and ended 2014-08-06.

Vote: Merge the intdiv operator patch into master? — Yes: 5 · No: 24 (closed)


Vote: If the operator vote fails to achieve a 2/3 majority, should the intdiv() function patch be merged into master? — Yes: 28 · No: 0 (closed)

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.

Changelog

  • 0.2 - Propose % % operator instead
  • 0.1 - Created, intdiv() function