Bug #66959 [Ver]: bcadd with floats fails

From: Date: Tue, 01 Apr 2014 13:47:05 +0000
Subject: Bug #66959 [Ver]: bcadd with floats fails
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185008@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66959&edit=1

 ID:                 66959
 Updated by:         nikic@php.net
 Reported by:        james dot turner dot phpninja at gmail dot com
 Summary:            bcadd with floats fails
 Status:             Verified
 Type:               Bug
-Package:            *Math Functions
+Package:            BC math related
 Operating System:   Ubuntu 13
 PHP Version:        5.5.10
 Block user comment: N
 Private report:     N

 New Comment:

BCMath explicitly works with strings, not floating point numbers. In https://bugs.php.net/bug.php?id=55160 Mike made the
decision that we should not have extra support for passing floats to bc functions.


Previous Comments:
------------------------------------------------------------------------
[2014-04-01 13:17:45] bwoebi@php.net

Actually the bcmath extension doesn't like scientific notation.

var_dump((string)-0.00000545);
string(8) "-5.45E-6"

and 

var_dump(bcadd("-0.05648055","-5.45E-6",9));
string(12) "-0.056480550"

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

Would be great if someone could make bcmath support scientific notation...

------------------------------------------------------------------------
[2014-03-26 12:08:25] james dot turner dot phpninja at gmail dot com

Description:
------------
bcadd does not appear to work for certain numbers when using floats as inputs rather than strings.

This is particularly apparent if you json_decode from json to php variables where floats are
generated as you get floats not strings. Any attempt at casting to a string produces the same
output.

I can only assume it's actually an issue with float->string casting.

Test script:
---------------
$a = -0.05648055;
$b = -0.00000545;

$c = bcadd($a,$b,9);
$d = $a+$b;

echo $c; // => -0.05648055  <- WRONG
echo $d; // => -0.056486    <- CORRECT

assert($c == -0.056486);

Expected result:
----------------
assert ok

Usage of string literals for the negative floats works fine. However casting the floats to strings
fails (which is why I assume that bcadd also has the same problem in casting to strings before
computation).

Actual result:
--------------
assert fail


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



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


Thread (7 messages)

« previous php.bugs (#185008) next »