note 27171 added to language.operators.arithmetic

From: Date: Tue, 26 Nov 2002 00:44:53 +0000
Subject: note 27171 added to language.operators.arithmetic
Groups: php.notes 
Request: Send a blank email to php-notes+get-39968@lists.php.net to get a copy of this message
Div-implementation - here's the final word: we test both algorithms with this series of calculations: 10'000 div 1'001 100'000 div 10'001 1'000'000 div 100'001 and so on. Of course, the answer should always be 9. Since we use standard precision floating point numbers, there will always be a point when a certain algorithm fails. Test 1: floor($a/$b) Works until 10000'000000'000000 div 1000'000000'000001 results in 9 when we add another 0, the answer jumps to 10 Test 2: function div($a,$b){return ($a-($a % $b))/$b} Works until 1000'000000 div 100000001 results in 9 when we add another 0, the answer jumps to 9.5899345834101 So there isn't only a flaw in Test 1, but in *both tests*. Furthermore, the flaw shows much much earlier in Test 2. Therefore, the easier implementation looks like the better and more stable one, too. I therefore suggest using floor($a/$b) to calculate divs. -- http://www.php.net/manual/en/language.operators.arithmetic.php http://master.php.net/manage/user-notes.php?action=edit+27171 http://master.php.net/manage/user-notes.php?action=delete+27171 http://master.php.net/manage/user-notes.php?action=reject+27171

« previous php.notes (#39968) next »