Edit report at http://bugs.php.net/bug.php?id=51233&edit=1
ID: 51233 Updated by: [email protected] 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: Please carefully read the link posted by Johannes. 138.95 can end to be represented as 138.4999999999999. The rest becomes obvious then. Previous Comments: ------------------------------------------------------------------------ [2010-03-08 13:33:35] [email protected] Due to their limited precision, you shouldn't use floating-point values for currency calculations. Ever. This is well established best practice across a variety of programming languages. You should use either integers to represent the value in cents (or whatever your smallest currency unit is) or an arbitrary precision library such as BCMath. Please note that this bug tracker isn't a support forum, and further questions on this would be better directed to a Web forum like Stack Overflow, the PHP general mailing list, a newsgroup, or IRC. ------------------------------------------------------------------------ [2010-03-08 13:20:45] daniel dot seif at castex dot de 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 ------------------------------------------------------------------------ [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
