#49300 [Bgs]: very bizarre loops with decimal numbers
| From: | scottmac@php.net | Date: | Thu, 20 Aug 2009 12:18:41 +0000 |
| Subject: | #49300 [Bgs]: very bizarre loops with decimal numbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-141306@lists.php.net to get a copy of this message | ||
ID: 49300
Updated by: scottmac@php.net
Reported By: looris at gmail dot com
Status: Bogus
Bug Type: Unknown/Other Function
Operating System: debian stable
PHP Version: 5.2.10
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2009-08-20 10:36:22] 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 08:58:58] 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-19 20:39:01] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49300&edit=1