Hi Andrea, On Tue, Jul 15, 2014 at 11:10 AM, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:
> 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? > If we are going to have integer arithmetics, it may be better to have full set of operators/functions https://bugs.php.net/bug.php?id=30701 Regards, -- Yasuo Ohgaki yohg...@ohgaki.net