Did I find a php bug?
January 12, 2007 by Emanuele Feronato
Filed Under Php •
Filed Under Php •
Maybe today I found a php bug... try to run this script on your server...
PHP:
-
<?php
-
-
$a=100.9;
-
$b = 100;
-
$c = $a-$b;
-
echo $c;
-
-
?>
What do you get?
I get a 0.90000000000001 on different servers with different php installations
Am I the only one?
Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
Tell me what do you think about this post. I'll write better and better entries.
10 Responses to “Did I find a php bug?”
Leave a Reply

No, you didn’t: http://www.php.net/float
….
So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the arbitrary precision math functions or gmp functions instead.
….
no your not the only one :p. I’m using php 5.1.6
That’s pretty common in many languages. I’ve encountered this behavior in Flash a few times too. It happens because floating point math isn’t completely accurate, and mathematical operations can cause you to lose precision. It’s not really the language, but the computer architecture.
That’s not a bug, http://php.net/float
Xera
Of what I remember it has something to do with this.
http://en.wikipedia.org/wiki/0.999
Hope it’s OK I testes the BBCodes.
[code]Test[/code][b]Test[/b][url]http://test.dk[/url]
I am taking programming languages class this semester. We are writing our programming language, compiler, interpreter so on. this is not a bug. I dont want to get depth about p.l. but I can say that php doesnt have data types. it allows you really flexible features, but if you are making calculations you have to handle your data
If it becomes a problem in programming, try using Math.round. And in your case, since you’re using tenths, try Math.round($a*10)/10. Hopefully that’ll fix it.
I get 0.9 with xampp server
I have tested it on my localhost (Using Apache, PHP & MySQL… *cough* AppServ *cough*) and mine reads 0.9 as the answer. :D
Actually, the 1 just shows it’s repeating. Like on a calculator it shows .00000001 or .111111112 or .2222222223, etc.