php BC math 高精度数学函数

简介

PHP 为任意精度数学计算提供了二进制计算器(Binary Calculator),它支持任意大小和精度的数字,以字符串形式描述。

需求

自 PHP 4.0.4 以来,libbcmath 都绑定在 PHP 内部。本扩展不需要其它外部库的支持。

安装

本类函数仅在 PHP 编译时配置了 --enable-bcmath 时可用。在 PHP 3 中,本类函数仅在 PHP 编译时没有配置 --disable-bcmath 时可用。

PHP 的 Windows版本已经内置该扩展模块的支持。无需加载任何附加扩展库即可使用这些函数。

运行时配置

这些函数的行为受 php.ini 的影响。

表格 1. BC 数学库配置选项

名称默认值可修改范围更新记录
bcmath.scale"0"PHP_INI_ALL自 PHP 5.0.0 起可用。
有关 PHP_INI_* 常量进一步的细节与定义参见 附录 G

以下是配置选项的简要解释。

bcmath.scale integer

所有 bcmath 函数中十进制数字的数目。参见 bcscale()

资源类型

本扩展模块未定义任何资源类型。

预定义常量

本扩展模块未定义任何常量。

目录 bcadd -- Add two arbitrary precision numbers bccomp -- Compare two arbitrary precision numbers bcdiv -- Divide two arbitrary precision numbers bcmod --  Get modulus of an arbitrary precision number bcmul -- Multiply two arbitrary precision number bcpow --  Raise an arbitrary precision number to another bcpowmod --  Raise an arbitrary precision number to another, reduced by a specified modulus bcscale --  Set default scale parameter for all bc math functions bcsqrt --  Get the square root of an arbitrary precision number bcsub --  Subtract one arbitrary precision number from another


bcadd()

<?php

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

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

?>

bccomp()
<?php

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

?>

bcdiv()

<?php

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

?>

bcmod()

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

bcmul()

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

bcpow()

<?php

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

?>

bcpowmod()

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

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

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

?>

bcscale()

<?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()

<?php

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

?>

bcsub()

<?php

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

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

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值