ID: 32003 User updated by: razorwarden-nobullshit at yahoo dot it Reported By: razorwarden-nobullshit at yahoo dot it -Status: Bogus +Status: Open Bug Type: Math related Operating System: Linux / win 2k PHP Version: 4.3.10 New Comment:
my question is... Why i have this problem with php ONLY?? It seems much like this is a php problem to me, not a floating numbers problem. And anyway, i can understand that if i do 2 / 3 i'll never have the exact value, but an approximated one. But -0.04 + 0.04 = 0.000000000000000012644358?? lol that's not a floating point fault, that's a php fault m8... Previous Comments: ------------------------------------------------------------------------ [2005-02-16 20:44:27] [EMAIL PROTECTED] Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. Thank you for your interest in PHP. ------------------------------------------------------------------------ [2005-02-16 19:35:54] razorwarden-nobullshit at yahoo dot it Description: ------------ PHP seems to have problems with floats... infact -0.04 + 0.04 is not equal to 1.3877787807814E-017... the operation $num = -0.04 + 0.04 itself returns 0 as expected but if we start from a bigger negative value (such as -4) and we add for several times a positive value (0.04), the result may vary and are quite weird. Note that this problem seems to vary from machine to machine. I've also tried the same code with c# and other languages and the output is the expected one, so there's no doubt that this is really a php bug. Reproduce code: --------------- <?php $start = -0.2; $end = 0.2; $step = 0.04; for ($i = $start; $i <= $end; $i += $i) { echo $i . "<br />\n"; } ?> Expected result: ---------------- -0.2 -0.16 -0.12 -0.08 -0.04 0 0.04 0.08 0.12 0.16 0.2 Actual result: -------------- -0.2 -0.16 -0.12 -0.08 -0.04 1.3877787807814E-017 0.04 0.08 0.12 0.16 note that there ISN'T the last number (0.2), most probably because the float value is smt like 2.000000000000001 With $start = -8 and $end = 8, the middle output is: [...] -0.31999999999999 -0.27999999999999 -0.23999999999999 -0.19999999999999 -0.15999999999999 -0.11999999999999 -0.079999999999993 -0.039999999999993 6.5641936330962E-015 0.040000000000007 0.080000000000007 0.12000000000001 0.16000000000001 0.20000000000001 0.24000000000001 0.28000000000001 [...] With my machine (win 2k, php 4.3.10), and [...] -0.08 -0.04 6.5641936330962E-015 0.04 0.08 [...] on my web server (linux, php 4.3.10 with modifications, see the links below for further details) A page with this code is online at: http://nemea.altervista.org/alfa/phpbug.php Complete php info at: http://nemea.altervista.org/alfa/phpinfo.php ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=32003&edit=1