Edit report at http://bugs.php.net/bug.php?id=51233&edit=1

 ID:               51233
 User updated by:  daniel dot seif at castex dot de
 Reported by:      daniel dot seif at castex dot de
 Summary:          Wrong results with float arithmetics
 Status:           Bogus
 Type:             Bug
 Package:          Scripting Engine problem
 Operating System: Multiple
 PHP Version:      5.3.2

 New Comment:

Thank you for the quick reply.



The string representation is not the problem here, though. 



We are calculating with monetary data and converting from one currency
to another. 



Imagine a two products, worth 138.00 and 0.95 with the need to keep only
2 decimal digits:



$totalPrice = floor((138.00 + 0.95) * 100) / 100;



The result should be 138.95, but PHP returns 138.94 ...



I consider this to be a problem on a worse level than just 'bogus'



Best Regards


Previous Comments:
------------------------------------------------------------------------
[2010-03-08 12:12:03] [email protected]

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-03-08 11:57:15] daniel dot seif at castex dot de

Description:
------------
When calculating with and rounding float values, the result of the
calculation is wrong.



This bug seems to affect multiple versions of php. I have tested it with
PHP 5.3.2 (Fedora 12), PHP 5.2.6 (Red Hat), PHP 5.3.0 (Windows XP).

Test script:
---------------
echo floor(138.95 * 100), "\n";

echo floor(141.95 * 100), "\n";

echo floor(142.95 * 100), "\n";



echo intval(142.95 * 100), "\n";

echo (int)(138.95 * 100);

Expected result:
----------------
13895

14195

14295

14295

13895

Actual result:
--------------
13894

14194

14294

14294

13894


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51233&edit=1

Reply via email to