Did I find a php bug?
Maybe today I found a php bug… try to run this script on your server…
1 2 3 4 5 6 7 8 | <?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?
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

























This post has 11 comments
eels
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.
….
sinatosk
no your not the only one :p. I’m using php 5.1.6
Josh
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.
Xera
That’s not a bug, http://php.net/float
Xera
V34
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]
ben
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
Shadow Scythe
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.
shedokan
I get 0.9 with xampp server
Dale Hay
I have tested it on my localhost (Using Apache, PHP & MySQL… *cough* AppServ *cough*) and mine reads 0.9 as the answer. :D
bob
Actually, the 1 just shows it’s repeating. Like on a calculator it shows .00000001 or .111111112 or .2222222223, etc.
shaman4d
This is bug not only in php, it is number round bug in all programming languages with float point math.