php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35986 Rounding error
Submitted: 2006-01-12 22:37 UTC Modified: 2006-01-12 22:47 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: nathan at pngwyn dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.1.2 OS: Linux
Private report: No CVE-ID: None
 [2006-01-12 22:37 UTC] nathan at pngwyn dot com
Description:
------------
Rounding error on all numbers between 2048 and 8191 ending with .575 - When rounding to two decimal places.

For Example, instead of rounding 2048.575 up to 2048.58 it is rounding down to 2048.57

See attached Code.

Example URL: http://www.pngwyn.com/test.php

* Note: Example URL is running 5.05 - But this has been tested on 5.1.2 as well.

Reproduce code:
---------------
<?php
$start = 2040.575;
$for = '6500';
$max = $for + $start;

print("<font face=verdana size=2><a href=\"http://www.pngwyn.com/phpinfo.php\">PHP Info for this Server</a><br><br>");

for($i = $start; $i <= $max; $i++) {
        $round = round($i, 2);
        print("Original Number: <b>$i</b> --- Rounded Number: <b>$round</b> <br>");
}
?>


Expected result:
----------------
Original Number: 2046.575 --- Rounded Number: 2046.58
Original Number: 2047.575 --- Rounded Number: 2047.58
Original Number: 2048.575 --- Rounded Number: 2048.58
Original Number: 2049.575 --- Rounded Number: 2049.58 
[ 2050.575 - 8189.575 ... ]
Original Number: 8190.575 --- Rounded Number: 8190.58
Original Number: 8191.575 --- Rounded Number: 8191.58
Original Number: 8192.575 --- Rounded Number: 8192.58
Original Number: 8193.575 --- Rounded Number: 8193.58

Actual result:
--------------
Original Number: 2046.575 --- Rounded Number: 2046.58
Original Number: 2047.575 --- Rounded Number: 2047.58
Original Number: 2048.575 --- Rounded Number: 2048.57
Original Number: 2049.575 --- Rounded Number: 2049.57 
[ 2050.575 - 8189.575 ... ]
Original Number: 8190.575 --- Rounded Number: 8190.57
Original Number: 8191.575 --- Rounded Number: 8191.57
Original Number: 8192.575 --- Rounded Number: 8192.58
Original Number: 8193.575 --- Rounded Number: 8193.58

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-12 22:47 UTC] tony2001@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 11 18:01:28 2024 UTC