Reputation:
The following code gives "Warning: bcdiv() [function.bcdiv]: Division by zero in ..."
$a = 20000000000000002;
$b = 20000000000000004;
echo bcdiv($a, $b);
Why does this happen?
If I put the values in "" then it doesn't give a warning.
Upvotes: 1
Views: 4386
Reputation: 3773
You were correct to wrap them in ""
as bcdiv wants the inputs as strings
string bcdiv ( string $left_operand , string $right_operand [, int $scale ] )
left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
scale
This optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().
From http://php.net/manual/en/function.bcdiv.php
Upvotes: 0
Reputation: 26732
string bcdiv ( string $left_operand , string $right_operand [, int $scale ] )
Reference - http://www.php.net/manual/en/function.bcdiv.php
Look at the parameters
and type
-
left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
scale
This optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().
Upvotes: 0
Reputation: 25965
You wrote your numbers as integers and in PHP those are way too high and are truncated to max possible integer value. BCMath works with strings. If you work with numbers that high, always make sure to put them in quotes to be sure that they really are strings.
Upvotes: 1
Reputation: 861
BCMath functions all accept strings as arguments, so putting them in quotes is what you want to do.
BCMath documentation can be found at: http://www.php.net/manual/en/book.bc.php
Upvotes: 0
Reputation: 33521
According to the docs, bcdiv
takes strings:
string bcdiv ( string $left_operand , string $right_operand [, int $scale ] )
Apparently, the integer values you are providing are too large for standard PHP ints to hold that value. bcmath
works on strings (which is actually not so strange):
For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.
(from the bcmath intro)
Upvotes: 0