ID: 41213 Updated by: [EMAIL PROTECTED] Reported By: jsbruns at selectionsheet dot com -Status: Open +Status: Bogus Bug Type: Math related Operating System: Windows NT Server PHP Version: 5.2.1 New Comment:
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. Previous Comments: ------------------------------------------------------------------------ [2007-04-27 13:55:45] jsbruns at selectionsheet dot com Description: ------------ Attempting to subtract one floating point number from another returns the incorrect result. Float 1 is assigned in the code, float 2 is retrieved from a mysql DB field, which is of type DECIMAL(5,4). When subtracting $float1 from $float2 the result is: -7.105427357601E-015 Reproduce code: --------------- //Field discount2 is defined as type DECIMAL(5.4). Original value in database is 0.56, use PHP to multiply by 100 to get a whole number (56). $result = mysql_query("SELECT `discount2` FROM `product_discounts` WHERE `product_id` = 'prod1'"); $discount2 = mysql_result($result)*100; $discount1 = 56; echo $discount1; //56 echo $discount2; //56 if ($discount2 != $discount1) echo "Discount1 not equal to Discount2"; //Y if (bccomp($discount1,$discount2) == 0) echo "Discount1 equal to Discount2"; //Y echo ($discount1 + $discount2); //112 echo ($discount1 - $discount2); //-7.105427357601E-015 Expected result: ---------------- When subtracting $discount1 from $discount2 I expected to get 0. When adding the two I get the expected 112. Actual result: -------------- When subtracting $discount1 from $discount2 I get: -7.105427357601E-015 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41213&edit=1