php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49300 very bizarre loops with decimal numbers
Submitted: 2009-08-19 20:39 UTC Modified: 2009-08-21 12:49 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: looris at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.10 OS: debian stable
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: looris at gmail dot com
New email:
PHP Version: OS:

 

 [2009-08-19 20:39 UTC] looris at gmail dot com
Description:
------------
Just look at the code.

Reproduce code:
---------------
<?php

for ($value=0.1; $value<0.6; $value+=0.1) {
echo $value."\n";
}

echo "\n";

for ($value=0.1; $value<0.8; $value+=0.1) {
echo $value."\n";
}

?>

Expected result:
----------------
0.1
0.2
0.3
0.4
0.5

0.1
0.2
0.3
0.4
0.5
0.6
0.7


Actual result:
--------------
0.1
0.2
0.3
0.4
0.5

0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-20 08:58 UTC] derick@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.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

.
 [2009-08-20 10:36 UTC] looris at gmail dot com
what are you talking about? string representation?

the second loop loops 8 times, when it should have looped only 7 times.

and using < or <= gives exactly the same result: it still loops for 0.8 when it shouldn't (since i wrote $value<0.8)

I assumed you would notice it but clearly you didn't, I'll be more descriptive next time.

We do agree that if $value is 0.8, the expression $value<0.8 is FALSE, now do we?
 [2009-08-20 12:18 UTC] scottmac@php.net
Unfortunately you're not understanding how floating point maths is represented with a computer. Read the link that derick pointed you to or have a quick look at wikipedia.
 [2009-08-20 12:43 UTC] looris at gmail dot com
unfortunatly, you do not understand how languages work.

a high level language, like php, must (MUST, not should!) abstract all the low level mechamisms.
a developer who writes php code must not be required to know how things work at levels below that of php.
if something unexpected like this happens, it's php's fault, not programmer's!

every time someone finds a thing that is clearly a bug (clearly to any sane person) and you state "it's working as intended", you only fool yourself.
what's the point in programming in a high level language, if that languages behaves in an unpredictable way, and its developers are happy with it?
I don't care how php handles floating point numbers internally, it may send all the data to a pair of monkey who do the math with pen and paper and then send it back to the program for all I care (and that at least would explain some oddities).

I perfectly know how floats are represented in computers, dude. I perfectly know that it's a rounding issue caused by the conversion from decimal to binary... I just don't care. While I code in php I expect the language to be fool-proof, and I expect it to solve this problems without me having to bother.
It can be done, just use a fractional representation for floats until you are forced to switch to a normal one. I mean, I know you never will, but it's still my duty to tell you what you should do.
 [2009-08-21 12:10 UTC] rasmus@php.net
Just curious, have you filed this bug against every other high-level language?  Because they all behave like this.  It is not PHP's job to invent a better way to store floating point values.  
 [2009-08-21 12:39 UTC] looris at gmail dot com
actually, it's supposed to do just this kind of thing.

compared to C, it allows you not to declare variables, not to malloc memory you want to use, not to destroy that memory, not to care about converting variables from one type to another, not to care about types at all.

it does a whole lot of things, all aimed at removing that burden from the developer, so yes, it would be perfectly natural for it to store floats in a much friendlier way when it's needed.
 [2009-08-21 12:49 UTC] rasmus@php.net
Well, all I can say is that you are in the minority.  PHP, Perl, Python, and Ruby all chose to rely on the system-level floating point implementation.  If you want to use precise integer math, you have that ability and simply divide by 10 for display purposes in your example.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 15:01:33 2024 UTC