php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40622 round function
Submitted: 2007-02-24 21:36 UTC Modified: 2007-02-25 15:10 UTC
From: bruno dot boccara at d2b dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.4.5 OS: windows 2003 Apache 2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
46 - 27 = ?
Subscribe to this entry?

 
 [2007-02-24 21:36 UTC] bruno dot boccara at d2b dot com
Description:
------------
round function gives wrong value.

Please do not send me to http://docs.sun.com/source/806-3568/ncg_goldberg.html, you have to admit that this is a bug !!!!!
if not, php CANNOT be use in production site with mathematical functions !!!

Reproduce code:
---------------
<?
// 140.25*(1-(34/100)) = 92.565


echo "formula = ". round(((140.25*(1-(34/100)))),2);echo "<br>";

echo "direct = ".round (92.565, 2);echo "<br>";

$valeur = 140.25*(1-(34/100));

echo "value = ".round($valeur,2);echo "<br>";

settype($valeur,"string");
settype($valeur,"float");

echo "value2 = ".round($valeur,2);

?>

Expected result:
----------------
formula = 92.57
direct = 92.57
value = 92.57
value2 = 92.57

Actual result:
--------------
formula = 92.56 // WRONG !
direct = 92.57
value = 92.56  // WRONG !
value2 = 92.57

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-24 22:05 UTC] johannes@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-02-25 15:10 UTC] bruno dot boccara at d2b dot com
Did I send an email to a robot ??
Each user who ask this question get the same answer.
You should be ashaimed to do this. You know that this is a a very big bug of PHP and no one is ready to resolve it !

This is not a way to do.
I'm very very desappointed.

For other users with this problem, you can do like me in the example to avoid this problem. This will slow down a little the result. Use the settype function to convert the number to string and then to float. I did'n test it in all cases, but it seems to work.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 02:01:29 2024 UTC