|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-04-27 14:42 UTC] tony2001@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 18 23:00:02 2025 UTC |
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