note 27171 deleted from language.operators.arithmetic by sniper

From: Date: Mon, 13 Oct 2003 00:08:33 +0000
Subject: note 27171 deleted from language.operators.arithmetic by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58388@lists.php.net to get a copy of this message
Note Submitter: michael at hotmail or so dot com ---- 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.

« previous php.notes (#58388) next »