php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32003 problem with floats...
Submitted: 2005-02-16 19:35 UTC Modified: 2005-02-17 08:13 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: razorwarden-nobullshit at yahoo dot it Assigned:
Status: Not a bug Package: Math related
PHP Version: 4CVS, 5CVS (2005-02-17) OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: razorwarden-nobullshit at yahoo dot it
New email:
PHP Version: OS:

 

 [2005-02-16 19:35 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-16 20:44 UTC] tony2001@php.net
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 23:16 UTC] razorwarden-nobullshit at yahoo dot it
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...
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jul 06 21:01:04 2020 UTC