note 24913 added to language.operators.arithmetic
| From: | saforres at alumni dot SPAMGUARD dot uwaterloo dot ca | Date: | Wed, 04 Sep 2002 18:53:34 +0000 |
| Subject: | note 24913 added to language.operators.arithmetic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-36120@lists.php.net to get a copy of this message | ||
niekomaatjes@hotmail.com wrote:
"To avoid this, you can add a real tiny fraction to the total, like this:
(int)($foo / $bar + 0.0000001)"
This sort of strategy will not work. Suppose $foo=9999999 and $bar=10000000. Then since
$foo<$bar, you would want int($foo/$bar) to return 0, but 9999999/10000000+0.0000001=1.000000000,
so (int)($foo / $bar + 0.0000001)=1.
It's not generally a good idea to rely on truncated floats for integer divisions.
--
http://www.php.net/manual/en/language.operators.arithmetic.php
http://master.php.net/manage/user-notes.php?action=edit+24913
http://master.php.net/manage/user-notes.php?action=delete+24913
http://master.php.net/manage/user-notes.php?action=reject+24913