note 69021 added to language.operators.arithmetic

From: Date: Sun, 20 Aug 2006 04:28:07 +0000
Subject: note 69021 added to language.operators.arithmetic
Groups: php.notes 
Request: Send a blank email to php-notes+get-116077@lists.php.net to get a copy of this message
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. ---- Server IP: 85.116.0.130 Probable Submitter: 82.71.54.164 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.operators.arithmetic.php Edit -- https://master.php.net/note/edit/69021 Del: integrated -- https://master.php.net/note/delete/69021/integrated Del: useless -- https://master.php.net/note/delete/69021/useless Del: bad code -- https://master.php.net/note/delete/69021/bad+code Del: spam -- https://master.php.net/note/delete/69021/spam Del: non-english -- https://master.php.net/note/delete/69021/non-english Del: in docs -- https://master.php.net/note/delete/69021/in+docs Del: other reasons-- https://master.php.net/note/delete/69021 Reject -- https://master.php.net/note/reject/69021 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116077) next »