Hi Andrea,

On Tue, Jul 15, 2014 at 9:14 AM, Andrea Faulds <a...@ajf.me> wrote:

> PHP currently lacks a way to do integer division. You can floor or int
> cast a floating-point division, but beyond 53-bits that produces the wrong
> result:
>
> $ sapi/cli/php -r 'var_dump((int)(PHP_INT_MAX / 3));'
> int(3074457345618258432)
>
> Furthermore, using a floating-point division isn’t really a proper way to
> do it; it’s a workaround for a lack of language support for a quite basic
> operation. This RFC proposes a function for integer division, intdiv(). It
> would work properly with 64-bit values:
>
> $ sapi/cli/php -r 'var_dump(intdiv(PHP_INT_MAX, 3));'
> int(3074457345618258602)
>

We have GMP object from 5.6 and we can do

[yohgaki@dev php-5.6]$ ./php-bin -r '$i = gmp_init('21342'); $a = $i / 3;
var_dump($a);'
object(GMP)#2 (1) {
  ["num"]=>
  string(4) "7114"
}
[yohgaki@dev php-5.6]$ ./php-bin -r '$i = gmp_init('21342'); $a = $i *
99999999; var_dump($a);'
object(GMP)#2 (1) {
  ["num"]=>
  string(13) "2134199978658"
}

IMHO, GMP is the choice when precise computation is needed as it could be
any number.
Native integer type is much faster for sure. Question is do we really need
it?

Regards,

--
Yasuo Ohgaki
yohg...@ohgaki.net

Reply via email to