note 69021 deleted from language.operators.arithmetic by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 15:05:12 +0000 |
| Subject: | note 69021 deleted from language.operators.arithmetic by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189806@lists.php.net to get a copy of this message | ||
Note Submitter: T Chan
----
Please note that floor($a/$b) is not the same as integer division. if the 'fractional
part' of $a/$b is close to 1, then it can round up, and floor($a/$b) will be larger than it
should be. The example is a bit contrived though (and printf doesn't print enough digits).
<?php printf("%f\n",$a = 9007199254740991); // 9007199254740991.000000
printf("%f\n", $b = $a * 3.0); //27021597764222970.000000, the actual value stored ends in
2 while the true value ends in 3
printf("%f\n", $b/$a - 3.0); //0.000000
printf("%f\n", $b - 2*$a - $a); //-1.000000
printf("%f\n",fmod($b,$a)); //9007199254740990.000000 fmod agrees too
?>
Admittedly it's a bit 'specially-crafted', but it just goes to show. I believe it
does work for integers that can be exactly represented in FP. But if you're dealing with ints
anyway, (a-a%b)/b has no function calls and returns an int, while floor() always returns a float.