Ref/bc-Phpdoc专题

bcadd

2个任意精度数字的加法计算

说明

string bcadd ( string $left_operand , <span class="type">string $right_operand [, <span class="methodparam">int $scale ] )

左操作数右操作数求和

参数

left_operand
左操作数,字符串类型

right_operand
右操作数,字符串类型

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

2个操作数求和之后的结果以字符串返回

范例

示例 #1 bcadd 示例

<?php

$a = '1.234';
$b = '5';

echo bcadd($a, $b);     // 6
echo bcadd($a, $b, 4);  // 6.2340

?>

参见

  • bcsub

bccomp

比较两个任意精度的数字

说明

int bccomp ( string $left_operand , <span class="type">string $right_operand [, <span class="methodparam">int $scale<span class="initializer"> = int ] )

right_operandleft_operand作比较, 并且返回一个整数的结果.

参数

left_operand
左边的运算数, 是一个字符串.

right_operand
右边的运算数, 是一个字符串.

scale
可选的scale参数被用作设置指示数字, 在使用来作比较的小数点部分.

返回值

如果两个数相等返回0, 左边的数left_operand比较右边的数right_operand大返回1, 否则返回-1.

范例

示例 #1 bccomp example

<?php

echo bccomp('1', '2') . "\n";   // -1
echo bccomp('1.00001', '1', 3); // 0
echo bccomp('1.00001', '1', 5); // 1

?>

bcdiv

2个任意精度的数字除法计算

说明

string bcdiv ( string $left_operand , <span class="type">string $right_operand [, <span class="methodparam">int $scale<span class="initializer"> = int ] )

左操作数除以右操作数

参数

left_operand
左操作数,字符串类型

right_operand
右操作数,字符串类型

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

返回结果为字符串类型的结果,如果右操作数是0结果为null

范例

示例 #1 bcdiv 示例

<?php

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

?>

参见

  • bcmul

bcmod

对一个任意精度数字取模

说明

string bcmod ( string $left_operand , <span class="type">string $modulus )

对左操作数使用系数取模

参数

left_operand
字符串类型的左操作数

modulus
字符串类型系数

返回值

返回字符串类型取模后结果,如果系数为0则返回null

范例

示例 #1 bcmod example

<?php
echo bcmod('4', '2'); // 0
echo bcmod('2', '4'); // 2
?>

参见

  • bcdiv

bcmul

2个任意精度数字乘法计算

说明

string bcmul ( string $left_operand , <span class="type">string $right_operand [, <span class="methodparam">int $scale<span class="initializer"> = int ] )

左操作数乘以右操作数

参数

left_operand
字符串类型的左操作数.

right_operand
字符串类型的右操作数.

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

返回结果为字符串类型.

范例

示例 #1 bcmul 示例

<?php
echo bcmul('1.34747474747', '35', 3); // 47.161
echo bcmul('2', '4'); // 8
?>

参见

  • bcdiv

bcpow

任意精度数字的乘方

说明

string bcpow ( string $left_operand , <span class="type">string $right_operand [, <span class="methodparam">int $scale ] )

左操作数右操作数次方运算.

参数

left_operand
字符串类型的左操作数.

right_operand
字符串类型的右操作数.

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

返回结果为字符串类型.

范例

示例 #1 bcpow 示例

<?php

echo bcpow('4.2', '3', 2); // 74.08

?>

注释

Note:

bcpow may return a result with fewer digits after the decimal point than the scale parameter would indicate. This only occurs when the result doesn't require all of the precision allowed by the scale. For example:

示例 #2 bcpow scale example

<?php
echo bcpow('5', '2', 2);     // prints "25", not "25.00"
?>

参见

  • bcpowmod
  • bcsqrt

bcpowmod

Raise an arbitrary precision number to another, reduced by a specified modulus

说明

string <span class="methodname">bcpowmod ( <span class="type">string $num , <span class="methodparam">string $exponent , string $modulus [, <span class="type">intnull $scale = null ] )

Use the fast-exponentiation method to raise num to the power exponent with respect to the modulus modulus.

参数

num
The base, as an integral string (i.e. the scale has to be zero).

exponent
The exponent, as an non-negative, integral string (i.e. the scale has to be zero).

modulus
The modulus, as an integral string (i.e. the scale has to be zero).

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

Returns the result as a string, or false if modulus is 0 or exponent is negative.

注释

Note:

Because this method uses the modulus operation, numbers which are not positive integers may give unexpected results.

更新日志

版本 说明
8.0.0 scale is now nullable.

范例

The following two statements are functionally identical. The <span class="function">bcpowmod version however, executes in less time and can accept larger parameters.

<?php
$a = bcpowmod($x, $y, $mod);

$b = bcmod(bcpow($x, $y), $mod);

// $a and $b are equal to each other.

?>

参见

  • bcpow
  • bcmod

bcscale

设置所有bc数学函数的默认小数点保留位数

说明

bool bcscale ( int $scale )

设置所有bc数学函数的未设定情况下得小数点保留位数.

参数

scale
小数点保留位数.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 bcscale example

<?php

// default scale : 3
bcscale(3);
echo bcdiv('105', '6.55957'); // 16.007

// this is the same without bcscale()
echo bcdiv('105', '6.55957', 3); // 16.007

?>

bcsqrt

任意精度数字的二次方根

说明

string bcsqrt ( string $operand [, <span class="type">int $scale ] )

返回操作数的二次方根.

参数

operand
字符串类型的操作数.

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

返回二次方根的结果为字符串类型,如果操作数是负数则返回null.

范例

示例 #1 bcsqrt 示例

<?php

echo bcsqrt('2', 3); // 1.414

?>

参见

  • bcpow

bcsub

2个任意精度数字的减法

说明

string bcsub ( string $left_operand , <span class="type">string $right_operand [, <span class="methodparam">int $scale<span class="initializer"> = int ] )

左操作数减去右操作数.

参数

left_operand
字符串类型的左操作数.

right_operand
字符串类型的右操作数.

scale
此可选参数用于设置结果中小数点后的小数位数。也可通过使用 <span class="function">bcscale 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

返回减法之后结果为字符串类型.

范例

示例 #1 bcsub example

<?php

$a = '1.234';
$b = '5';

echo bcsub($a, $b);     // -3
echo bcsub($a, $b, 4);  // -3.7660

?>

参见

  • bcadd

目录

  • bcadd — 2个任意精度数字的加法计算
  • bccomp — 比较两个任意精度的数字
  • bcdiv — 2个任意精度的数字除法计算
  • bcmod — 对一个任意精度数字取模
  • bcmul — 2个任意精度数字乘法计算
  • bcpow — 任意精度数字的乘方
  • bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
  • bcscale — 设置所有bc数学函数的默认小数点保留位数
  • bcsqrt — 任意精度数字的二次方根
  • bcsub — 2个任意精度数字的减法

本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除