From:             jsbruns at selectionsheet dot com
Operating system: Windows NT Server
PHP version:      5.2.1
PHP Bug Type:     Math related
Bug description:  Subtracting 2 floats returns incorrect results

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 bug report at http://bugs.php.net/?id=41213&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=41213&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=41213&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=41213&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=41213&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=41213&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=41213&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=41213&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=41213&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=41213&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=41213&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=41213&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=41213&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=41213&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=41213&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=41213&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=41213&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=41213&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=41213&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=41213&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=41213&r=mysqlcfg

Reply via email to