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?
11 Responses to “Did I find a php bug?”
Leave a Reply
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)





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.
This is bug not only in php, it is number round bug in all programming languages with float point math.