bcdiv

(PHP 3, PHP 4, PHP 5)

bcdiv -- Divide two arbitrary precision numbers

Description

string bcdiv ( string left_operand, string right_operand [, int scale] )

Divides the left_operand by the right_operand and returns the result. The optional scale sets the number of digits after the decimal place in the result, which defaults to 0.

范例

例子 1. bcdiv() example

<?php

echo bcdiv('105', '6.55957', 3);  // 16.007

?>

参见

bcmul().


add a note add a note User Contributed Notes
Robert Lozyniak
06-May-2006 07:05
Let me see if I can clarify:

If you pass a _string_ such as "1E2" to a BC math function, it will stop reading as soon as it sees the "E". (The BC math functions do not know scientific notation.)

If you set the variable $foo equal to the _numeric_ 1E2, the variable $foo will then contain the mathematical number known as "one hundred".

If you then pass $foo to a BC math function, PHP will convert the contents of $foo (the mathematical number one hundred) into the string "100". This is the same exact behaviour you would get if you tried to evaluate $foo." apples": you have something which expects a string but you are passing it a number, so the number gets converted to a string.

However, if you try to pass a googol (or rather, the closest IEEE double-precision floating-point number to a googol) to BC math this way, I would not expect it to work. Why? Because PHP would try to write that googol (excuse me, approximation to a googol) in scientific notation (it would, wouldn't it? I'm too lazy to try it), and BC math does not know scientific notation. So there.
artmine77 at yahoo dot com
10-Oct-2005 01:47
Block 1:
{
$arg1="1E+11";
$arg2="5";
$ret=bcdiv($arg1,$arg2,0); // $ret=0  (calculation error)
}
Block 2:
{
$arg1=1E+11;
$arg2=5;
$ret=bcdiv($arg1,$arg2,0); // $ret=20000000000
}
Why do the manual says that $arg1 and $arg2 should be strings when that's when the error happens? If someone knows, I would appreciate the answer.
cristianDOTzuddas]NOSPAM[gmailDOTcom
24-Jul-2005 08:10
Decimal to binary conversion, using BC Math.
Note: this function is VERY slow if the decimal number is too big!

<?
function bc_decbin($dec_str) {
   if (
strlen($dec_str)>0) {
      
$bin_str = '';
       do {
           if (((int)
$dec_str[strlen($dec_str)-1] % 2) === 0)
              
$bin_str .= '0';
           else
              
$bin_str .= '1';
          
          
$dec_str = bcdiv($dec_str, '2');
       } while (
$dec_str!='0');
      
       return
strrev($bin_str);
   }
   else
       return
null;
}
?>

-----
Cristian
www.CodeFlower.com