Did I find a php bug?

Maybe today I found a php bug... try to run this script on your server...

PHP:
  1. <?php
  2.  
  3. $a=100.9;
  4. $b = 100;
  5. $c = $a-$b;
  6. echo $c;
  7.  
  8. ?>

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.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

10 Responses to “Did I find a php bug?”

  1. eels on January 12th, 2007 9:55 pm

    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.
    ….

  2. sinatosk on January 12th, 2007 10:04 pm

    no your not the only one :p. I’m using php 5.1.6

  3. Josh on January 13th, 2007 2:18 am

    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.

  4. Xera on January 18th, 2007 2:57 pm

    That’s not a bug, http://php.net/float

    Xera

  5. V34 on February 17th, 2007 5:58 pm

    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]

  6. ben on November 28th, 2007 5:48 am

    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

  7. Shadow Scythe on December 1st, 2007 12:28 pm

    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.

  8. shedokan on January 25th, 2008 5:19 pm

    I get 0.9 with xampp server

  9. Dale Hay on February 3rd, 2008 4:01 am

    I have tested it on my localhost (Using Apache, PHP & MySQL… *cough* AppServ *cough*) and mine reads 0.9 as the answer. :D

  10. bob on February 6th, 2008 7:55 pm

    Actually, the 1 just shows it’s repeating. Like on a calculator it shows .00000001 or .111111112 or .2222222223, etc.

Leave a Reply