Reputation: 1871
Let's say I have
$foo = bcsub(bcdiv(1, 3, 20), 0.00001, 20);
it returns me 0.33333333333333333333
If I have
$foo = bcsub(bcdiv(1, 3, 20), 0.0001, 20);
it returns me 0.33323333333333333332
If I have
$foo = bcsub(0.333333333333333333, 0.00001, 20);
it returns me 0.33333333333333331483
If I have
$foo = bcsub(0.333333333333333333, 0.0001, 20);
it returns me 0.33323333333333331482
So why it can't properly subtract, it's something with floating point? But it works fine when just bcdiv(1, 3, 20)
Upvotes: 2
Views: 1081
Reputation: 75619
Use strings instead of floats as parameters to the BC functions:
$foo = bcsub(bcdiv("1", "3", "20"), "0.00001", "20");
If you use a float (i.e. 0.00001), PHP will convert this number to a float, which is not precise. If you use a string (i.e. "0.00001"), BC will do the conversion to an arbitrary precision number, which is precise.
Upvotes: 9