1

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.

Jacob Tomlinson
  • 3,341
  • 2
  • 31
  • 62

5 Answers5

1

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.

silkfire
  • 24,585
  • 15
  • 82
  • 105
0
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().

Jacob Tomlinson
  • 3,341
  • 2
  • 31
  • 62
swapnesh
  • 26,318
  • 22
  • 94
  • 126
0

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)

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

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

shannonman
  • 841
  • 4
  • 8
0

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

Jacob Tomlinson
  • 3,341
  • 2
  • 31
  • 62