From:             razorwarden-nobullshit at yahoo dot it
Operating system: Linux / win 2k
PHP version:      4.3.10
PHP Bug Type:     Math related
Bug description:  problem with floats...

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 bug report at http://bugs.php.net/?id=32003&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=32003&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=32003&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=32003&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=32003&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=32003&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=32003&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=32003&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=32003&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=32003&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=32003&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=32003&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=32003&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=32003&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=32003&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=32003&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=32003&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=32003&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=32003&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=32003&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=32003&r=mysqlcfg

Reply via email to