Bug #66959 [Ver]: bcadd with floats fails

From: Date: Wed, 13 Jul 2016 23:46:44 +0000
Subject: Bug #66959 [Ver]: bcadd with floats fails
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202292@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:         cmb@php.net
 Reported by:        james dot turner dot phpninja at gmail dot com
 Summary:            bcadd with floats fails
 Status:             Verified
 Type:               Bug
 Package:            BC math related
 Operating System:   Ubuntu 13
 PHP Version:        5.5.10
 Block user comment: N
 Private report:     N

 New Comment:

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

There would still be the issue that the string cast depends on the
locale, e.g.

  <?php
  setlocale(LC_NUMERIC, 'de_DE.UTF-8');
  // ...
  var_dump(bcadd(-0.05648055, -0.05648055, 8));

outputs

  string(10) "0.00000000"


Previous Comments:
------------------------------------------------------------------------
[2014-04-02 22:04:09] trainmaster at gmx dot net

Some more examples (Output for 5.2.2 - 5.6.0alpha3):

var_dump(bcadd(0,-0.00555555,9));
// string(12) "-0.005555550"
var_dump((string) -0.00555555);
// string(11) "-0.00555555"

var_dump(bcadd(0,-0.00055555,9));
// string(12) "-0.000555550"
var_dump((string) -0.00055555);
// string(11) "-0.00055555"

var_dump(bcadd(0,-0.00005555,9));
// string(11) "0.000000000"
var_dump((string) -0.00005555);
// string(10) "-5.555E-05"

var_dump(bcadd(0,-0.00000555,9));
// string(11) "0.000000000"
var_dump((string) -0.00000555);
// string(9) "-5.55E-06"

var_dump(bcadd(0,-0.00000055,9));
// string(11) "0.000000000"
var_dump((string) -0.00000055);
// string(8) "-5.5E-07"

var_dump(bcadd(0,-0.00000005,9));
// string(11) "0.000000000"
var_dump((string) -0.00000005);
// string(6) "-5E-08"

------------------------------------------------------------------------
[2014-04-02 21:56:23] trainmaster at gmx dot net

@nikic

See bwoebi's example when string arguments are passed. The problem seems to be the scientific
notation.

------------------------------------------------------------------------
[2014-04-01 13:47:04] nikic@php.net

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.

------------------------------------------------------------------------
[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 (#202292) next »