<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'book.bc.php',
    1 => 'BC Math',
    2 => 'BCMath 任意精度数学',
  ),
  'up' => 
  array (
    0 => 'refs.math.php',
    1 => '数学扩展',
  ),
  'prev' => 
  array (
    0 => 'refs.math.php',
    1 => '数学扩展',
  ),
  'next' => 
  array (
    0 => 'bc.setup.php',
    1 => '安装/配置',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    '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 任意精度数学</h1>
 

 
 <div id="intro.bc" class="preface">
  <h1 class="title">简介</h1>
  <p class="para">
    对于任意精度的数学， 如果有足够多的内存，PHP 提供的 BCMath 支持用字符串的形式表示任意大小和精度的数字，最大尺寸为
   <code class="literal">2147483647</code>（即 <code class="literal">0x7FFFFFFF</code>）。
  </p>
  <p class="para">
   有效（即格式良好）的 BCMath 数字是匹配正则表达式
   <code class="literal">/^[+-]?[0-9]*(\.[0-9]*)?$/</code> 的字符串。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    BCMath 函数想要操作的是 <span class="type"><a href="language.types.string.php" class="type string">string</a></span>，如果传入了 <span class="type"><a href="language.types.float.php" class="type float">float</a></span> 可能无法达到想要的效果，
    因为 PHP 会将 <span class="type"><a href="language.types.float.php" class="type float">float</a></span> 的值转换为 <span class="type"><a href="language.types.string.php" class="type string">string</a></span>。
    也就意味着 <span class="type"><a href="language.types.string.php" class="type string">string</a></span> 可能会带科学计数法符号（BCMath 并不支持），
    另外在 PHP 8.0.0 之前，小数点分隔符也不支持本地化（BCMath 只能识别英文句号小数点）。
   </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">// 使用逗号小数点<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">安装/配置</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="bc.installation.php">安装</a></li><li><a href="bc.configuration.php">运行时配置</a></li></ul></li><li><a href="ref.bc.php">BC 数学 函数</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="function.bcadd.php">bcadd</a> — 两个任意精度数字的加法计算</li><li><a href="function.bcceil.php">bcceil</a> — Round up arbitrary precision number</li><li><a href="function.bccomp.php">bccomp</a> — 比较两个任意精度的数字</li><li><a href="function.bcdiv.php">bcdiv</a> — 两个任意精度的数字除法计算</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> — 任意精度数字取模</li><li><a href="function.bcmul.php">bcmul</a> — 两个任意精度数字乘法计算</li><li><a href="function.bcpow.php">bcpow</a> — 任意精度数字的乘方</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> — 设置/获取所有 bc math 函数的默认小数点保留位数</li><li><a href="function.bcsqrt.php">bcsqrt</a> — 任意精度数字的二次方根</li><li><a href="function.bcsub.php">bcsub</a> — 两个任意精度数字的减法</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); ?>