#40892 [Bgs]: floating point rounding issue
| From: | fizz at beyond dot hjsoft dot com | Date: | Thu, 22 Mar 2007 14:42:12 +0000 |
| Subject: | #40892 [Bgs]: floating point rounding issue | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-110682@lists.php.net to get a copy of this message | ||
ID: 40892
User updated by: fizz at beyond dot hjsoft dot com
Reported By: fizz at beyond dot hjsoft dot com
Status: Bogus
Bug Type: Math related
Operating System: linux, solaris
PHP Version: 5.2.1
New Comment:
I very much understand that, my concern was more of a consistency
issue between how echo and var_dump does it (which outputs the
correct information) and how printf/sprintf does (which gives
incorrect output).
Previous Comments:
------------------------------------------------------------------------
[2007-03-22 14:23:26] 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.
------------------------------------------------------------------------
[2007-03-22 14:13:24] fizz at beyond dot hjsoft dot com
Description:
------------
When printing out a floating point number using printf (or storing
it with sprintf, or casting it as an int) it gives an incorrect
number. This appears to be due to floating point math being
inaccurate.
Reproduce code:
---------------
<?php
$val = 309;
for ($x = 0; $x < 950; $x++)
$val += 0.001;
var_dump($val);
echo $val . "\n";
$val = $val * 100;
var_dump($val);
echo $val . "\n";
printf ("%d\n", $val);
echo (int)$val . "\n";
?>
Expected result:
----------------
float(309.95)
309.95
float(30995)
30995
30995
30995
Actual result:
--------------
float(309.95)
309.95
float(30995)
30995
30994
30994
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40892&edit=1