SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question Is this poor maths or a php bug?

    Hello, I have been testing some validation and I cant work out why this isnt working.

    PHP Code:
    $value_to_check 25.98;

    $value_created_for_check 19.99 5.99;

    $value_created_for_check $value_created_for_check 1;

    // this validation statement fails
    if ($value_to_check != $value_created_for_check) {/*fail validation*/}

    // but this one passes
    if ($value_to_check != round($value_created_for_check2)) {/*fail validation*/}

    // but in both validation statements both the values print as 25.98 
    I am just confused as to why they dont both pass validation, because as far as I can see mathematically speaking 19.99 + 5.99 = 25.98 exactly with no rounding up required?

    thanks ro0bear

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Welcome to floating point math.

  3. #3
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Floating point representation of numbers in computers are approximate.

    PHP Code:
    echo $value_to_check $value_created_for_check
    Will not print 0, but some really tiny number, which is causing the problem.

    Easiest way to prevent this to work in the smallest unit of currency (cents, pennies, or whatever it maybe)

    PHP Code:
    value_to_check 2598
    $value_created_for_check 1999 599

    $value_created_for_check $value_created_for_check 1

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Basically all of the numers in the computer are stored in binary so in oder to do the calculations the decimal values entered are converted to the closest binary equivalents. The calculations are then done and the result converted back to the closest decimal equivalent. The small difference in the expected result is due th the decimal and binary values not being the exact same value (since the computer can only store a certain number of digits).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Non-Member
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    623
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Basically all of the numers in the computer are stored in binary so in oder to do the calculations the decimal values entered are converted to the closest binary equivalents. The calculations are then done and the result converted back to the closest decimal equivalent. The small difference in the expected result is due th the decimal and binary values not being the exact same value (since the computer can only store a certain number of digits).
    Oh I see, that makes sense now. Thanks everyone for clearing that up, I thought I was going mad lol.

  6. #6
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it becomes an issue then you may try BCMath.

  7. #7
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case you haven't picked up on this, a basic rule of thumb is don't compare floats.

    Read http://us2.php.net/float


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •