php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52315 Error in type convert after specific round() operation
Submitted: 2010-07-12 14:40 UTC Modified: 2010-07-13 18:27 UTC
From: wisent at yandex dot ru Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.13 OS: Ubuntu 8.04
Private report: No CVE-ID: None
 [2010-07-12 14:40 UTC] wisent at yandex dot ru
Description:
------------
First I perform specific round() operation. Then I convert the number from float to integer. The result is wrong, the number differs by 1 from what it should be.

The problem appears not for every number.

Test script:
---------------
    // this works bad:
    $sum = '4926.11';
    $sum = round($sum, 2) * 100;
    var_dump($sum);                // float(492611)

    $sum = (integer) $sum;
    var_dump($sum);                // int(492610)

    // the same round() operation, the other number, works good
    $sum = '426.11';
    $sum = round($sum, 2) * 100;
    var_dump($sum);                // float(42611)

    $sum = (integer) $sum;
    var_dump($sum);                // int(42611)

    // the first numbers, the other round() operation, works good
    $sum = '4926.11';
    $sum = round($sum * 100);
    var_dump($sum);                // float(492611)

    $sum = (integer) $sum;
    var_dump($sum);                // int(492611)





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-13 18:27 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-07-13 18:27 UTC] aharvey@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 [2010-07-14 11:07 UTC] wisent at yandex dot ru
aharvey, the problem is not related with strings.

I better should write the code like this:

    // this works bad:
    $sum = 4926.11;
    $sum = round($sum, 2) * 100;
    var_dump($sum);                // float(492611)

    $sum = (integer) $sum;
    var_dump($sum);                // int(492610)

    // the first numbers, the other round() operation, works good
    $sum = 4926.11;
    $sum = round($sum * 100);
    var_dump($sum);                // float(492611)

    $sum = (integer) $sum;
    var_dump($sum);                // int(492611)

You probably haven't noticed the main point. After operations on variable with round() function variables have the same representation, float(492611). But the subsequent type conversion to integer returns different values, int(492610) and int(492611).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 22 10:01:30 2024 UTC