<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.math.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'book.bc.php',
    1 => 'BC Math',
    2 => 'BCMath Arbitrary Precision Mathematics',
  ),
  'up' => 
  array (
    0 => 'refs.math.php',
    1 => 'Mathematical Extensions',
  ),
  'prev' => 
  array (
    0 => 'refs.math.php',
    1 => 'Mathematical Extensions',
  ),
  'next' => 
  array (
    0 => 'bc.setup.php',
    1 => 'Installing/Configuring',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/bc/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.bc.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.bc" class="book">
 
 <h1 class="title">BCMath Arbitrary Precision Mathematics</h1>
 

 
 <div id="intro.bc" class="preface">
  <h1 class="title">Introduction</h1>
  <p class="para">
   For arbitrary precision mathematics PHP offers BCMath which
   supports numbers of any size and precision up to <code class="literal">2147483647</code> (or <code class="literal">0x7FFFFFFF</code>) decimal digits,
   if there is sufficient memory, represented as strings.
  </p>
  <p class="para">
   Valid (aka. well-formed) BCMath numbers are strings which match the regular expression
   <code class="literal">/^[+-]?[0-9]*(\.[0-9]*)?$/</code>.
  </p>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    Passing values of type <span class="type"><a href="language.types.float.php" class="type float">float</a></span> to a BCMath function which expects
    a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> as operand may not have the desired effect due to the
    way PHP converts <span class="type"><a href="language.types.float.php" class="type float">float</a></span> values to <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, namely
    that the <span class="type"><a href="language.types.string.php" class="type string">string</a></span> may be in exponential notation (which is not
    supported by BCMath), and that, prior to PHP 8.0.0, the decimal separator is locale dependent
    (while BCMath always expects a decimal point).
   </p>
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$num1 </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #FF8000">// (string) 0 =&gt; '0'<br /></span><span style="color: #0000BB">$num2 </span><span style="color: #007700">= -</span><span style="color: #0000BB">0.000005</span><span style="color: #007700">; </span><span style="color: #FF8000">// (string) -0.000005 =&gt; '-5.05E-6'<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">bcadd</span><span style="color: #007700">(</span><span style="color: #0000BB">$num1</span><span style="color: #007700">, </span><span style="color: #0000BB">$num2</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">); </span><span style="color: #FF8000">// =&gt; '0.000000'<br /><br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_NUMERIC</span><span style="color: #007700">, </span><span style="color: #DD0000">'de_DE'</span><span style="color: #007700">); </span><span style="color: #FF8000">// uses a decimal comma<br /></span><span style="color: #0000BB">$num2 </span><span style="color: #007700">= </span><span style="color: #0000BB">1.2</span><span style="color: #007700">; </span><span style="color: #FF8000">// (string) 1.2 =&gt; '1,2'<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">bcsub</span><span style="color: #007700">(</span><span style="color: #0000BB">$num1</span><span style="color: #007700">, </span><span style="color: #0000BB">$num2</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// =&gt; '0.0'<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </div>
 </div>
 

 







 







 





<ul class="chunklist chunklist_book"><li><a href="bc.setup.php">Installing/Configuring</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="bc.installation.php">Installation</a></li><li><a href="bc.configuration.php">Runtime Configuration</a></li></ul></li><li><a href="ref.bc.php">BC Math Functions</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="function.bcadd.php">bcadd</a> — Add two arbitrary precision numbers</li><li><a href="function.bcceil.php">bcceil</a> — Round up arbitrary precision number</li><li><a href="function.bccomp.php">bccomp</a> — Compare two arbitrary precision numbers</li><li><a href="function.bcdiv.php">bcdiv</a> — Divide two arbitrary precision numbers</li><li><a href="function.bcdivmod.php">bcdivmod</a> — Get the quotient and modulus of an arbitrary precision number</li><li><a href="function.bcfloor.php">bcfloor</a> — Round down arbitrary precision number</li><li><a href="function.bcmod.php">bcmod</a> — Get modulus of an arbitrary precision number</li><li><a href="function.bcmul.php">bcmul</a> — Multiply two arbitrary precision numbers</li><li><a href="function.bcpow.php">bcpow</a> — Raise an arbitrary precision number to another</li><li><a href="function.bcpowmod.php">bcpowmod</a> — Raise an arbitrary precision number to another, reduced by a specified modulus</li><li><a href="function.bcround.php">bcround</a> — Round arbitrary precision number</li><li><a href="function.bcscale.php">bcscale</a> — Set or get default scale parameter for all bc math functions</li><li><a href="function.bcsqrt.php">bcsqrt</a> — Get the square root of an arbitrary precision number</li><li><a href="function.bcsub.php">bcsub</a> — Subtract one arbitrary precision number from another</li></ul></li><li><a href="class.bcmath-number.php">BcMath\Number</a> — The BcMath\Number class<ul class="chunklist chunklist_book chunklist_children"><li><a href="bcmath-number.add.php">BcMath\Number::add</a> — Adds an arbitrary precision number</li><li><a href="bcmath-number.ceil.php">BcMath\Number::ceil</a> — Rounds up an arbitrary precision number</li><li><a href="bcmath-number.compare.php">BcMath\Number::compare</a> — Compares two arbitrary precision numbers</li><li><a href="bcmath-number.construct.php">BcMath\Number::__construct</a> — Creates a BcMath\Number object</li><li><a href="bcmath-number.div.php">BcMath\Number::div</a> — Divides by an arbitrary precision number</li><li><a href="bcmath-number.divmod.php">BcMath\Number::divmod</a> — Gets the quotient and modulus of an arbitrary precision number</li><li><a href="bcmath-number.floor.php">BcMath\Number::floor</a> — Rounds down an arbitrary precision number</li><li><a href="bcmath-number.mod.php">BcMath\Number::mod</a> — Gets the modulus of an arbitrary precision number</li><li><a href="bcmath-number.mul.php">BcMath\Number::mul</a> — Multiplies an arbitrary precision number</li><li><a href="bcmath-number.pow.php">BcMath\Number::pow</a> — Raises an arbitrary precision number</li><li><a href="bcmath-number.powmod.php">BcMath\Number::powmod</a> — Raises an arbitrary precision number, reduced by a specified modulus</li><li><a href="bcmath-number.round.php">BcMath\Number::round</a> — Rounds an arbitrary precision number</li><li><a href="bcmath-number.serialize.php">BcMath\Number::__serialize</a> — Serializes a BcMath\Number object</li><li><a href="bcmath-number.sqrt.php">BcMath\Number::sqrt</a> — Gets the square root of an arbitrary precision number</li><li><a href="bcmath-number.sub.php">BcMath\Number::sub</a> — Subtracts an arbitrary precision number</li><li><a href="bcmath-number.tostring.php">BcMath\Number::__toString</a> — Converts BcMath\Number to string</li><li><a href="bcmath-number.unserialize.php">BcMath\Number::__unserialize</a> — Deserializes a data parameter into a BcMath\Number object</li></ul></li></ul></div><?php manual_footer($setup); ?>