伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: BC Math

来源:网络转载 浏览:30627次 时间:2024-06-30
简介 » « 数学扩展
  • PHP 手册
  • 函数参考
  • 数学扩展

BCMath 任意精度数学

  • 简介
  • 安装/配置
    • 需求
    • 安装
    • 运行时配置
    • 资源类型
  • 预定义常量
  • BC 数学 函数
    • bcadd — 两个任意精度数字的加法计算
    • bccomp — 比较两个任意精度的数字
    • bcdiv — 两个任意精度的数字除法计算
    • bcmod — 任意精度数字取模
    • bcmul — 两个任意精度数字乘法计算
    • bcpow — 任意精度数字的乘方
    • bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
    • bcscale — 设置/获取所有 bc math 函数的默认小数点保留位数
    • bcsqrt — 任意精度数字的二次方根
    • bcsub — 两个任意精度数字的减法
add a note

User Contributed Notes 6 notes

up down 56 Hayley Watson6 years ago This extension is an interface to the GNU implementation as a library of the Basic Calculator utility by Philip Nelson; hence the name. up down 21 volek at adamv dot cz7 years ago Note that when you use implementation of factorial that ClaudiuS made, you get results even if you try to calculate factorial of number that you normally can't, e.g. 2.5, -2, etc. Here is safer implementation:
<?php
/**
* Calculates a factorial of given number.
* @param string|int $num
* @throws InvalidArgumentException
* @return string
*/
function bcfact($num)
{
    if (!filter_var($num, FILTER_VALIDATE_INT) || $num <= 0) {
        throw new InvalidArgumentException(sprintf('Argument must be natural number, "%s" given.', $num));
    }

    for ($result = '1'; $num > 0; $num--) {
        $result = bcmul($result, $num);
    }

    return $result;
}
?>
up down 18 ClaudiuS9 years ago Needed to compute some permutations and found the BC extension great but poor on functions, so untill this gets implemented here's the factorial function:

<?php
/* BC FACTORIAL
* n! = n * (n-1) * (n-2) .. 1 [eg. 5! = 5 * 4 * 3 * 2 * 1 = 120]
*/
function bcfact($n){
    $factorial=$n;
    while (--$n>1) $factorial=bcmul($factorial,$n);
    return $factorial;
}

print bcfact(50);
//30414093201713378043612608166064768844377641568960512000000000000
?>
up down -12 Artur Kuritsyn4 years ago Useful function for bcround

    function getBcRound($number, $precision = 0)
    {
        $precision = ($precision < 0)
                   ? 0
                   : (int) $precision;
        if (strcmp(bcadd($number, '0', $precision), bcadd($number, '0', $precision+1)) == 0) {
            return bcadd($number, '0', $precision);
        }
        if (getBcPresion($number) - $precision > 1) {
            $number = getBcRound($number, $precision + 1);
        }
        $t = '0.' . str_repeat('0', $precision) . '5';
        return $number < 0
               ? bcsub($number, $t, $precision)
               : bcadd($number, $t, $precision);
    }
   
    function getBcPresion($number) {
        $dotPosition = strpos($number, '.');
        if ($dotPosition === false) {
            return 0;
        }
        return strlen($number) - strpos($number, '.') - 1;
    }
   
    var_dump(getBcRound('3', 0) == number_format('3', 0));
    var_dump(getBcRound('3.4', 0) == number_format('3.4', 0));
    var_dump(getBcRound('3.56', 0) == number_format('3.6', 0));
    var_dump(getBcRound('1.95583', 2) == number_format('1.95583', 2));
    var_dump(getBcRound('5.045', 2) == number_format('5.045', 2));
    var_dump(getBcRound('5.055', 2) == number_format('5.055', 2));
    var_dump(getBcRound('9.999', 2) == number_format('9.999', 2));
    var_dump(getBcRound('5.0445', 5) == number_format('5.044500', 5));
    var_dump(getBcRound('5.0445', 4) == number_format('5.04450', 4));
    var_dump(getBcRound('5.0445', 3) == number_format('5.0445', 3));
    var_dump(getBcRound('5.0445', 2) == number_format('5.045', 2));
    var_dump(getBcRound('5.0445', 1) == number_format('5.05', 1));
    var_dump(getBcRound('5.0445', 0) == number_format('5.0', 0));//
    var_dump(getBcRound('5.04455', 2) == number_format('5.045', 2));
    var_dump(getBcRound('99.999', 2) == number_format('100.000', 2));
    var_dump(getBcRound('99.999') == number_format('99.999', 0));
    var_dump(getBcRound('99.999', 'a') == number_format('99.999', 0));
    var_dump(getBcRound('99.999', -1.5) == number_format('99.999', 0));
    var_dump(getBcRound('-0.00001', 2) == number_format('-0.000', 2));
    var_dump(getBcRound('-0.0000', 2) == number_format('0', 2));
    var_dump(getBcRound('-4.44455', 2) == number_format('-4.445', 2));
    var_dump(getBcRound('-4.44555', 0) == number_format('-4.5', 0));
    var_dump(getBcRound('-4.444444444444444444444444444444444444444444445', 0) == number_format('-4.5', 0));
up down -21 George Lund8 years ago It's worth noting that this library is named very wrongly.

It may be called 'Binary Calculator', but what you're getting is a decimal calculator that can represent base-10 fractions accurately.
up down -14 Anonymous4 years ago Useful function for bcround

    function getBcRound($number, $precision = 0)
    {
        $precision = ($precision < 0)
                   ? 0
                   : (int) $precision;
        if (strcmp(bcadd($number, '0', $precision), bcadd($number, '0', $precision+1)) == 0) {
            return bcadd($number, '0', $precision);
        }
        if (getBcPresion($number) - $precision > 1) {
            $number = getBcRound($number, $precision + 1);
        }
        $t = '0.' . str_repeat('0', $precision) . '5';
        return $number < 0
               ? bcsub($number, $t, $precision)
               : bcadd($number, $t, $precision);
    }
   
    function getBcPresion($number) {
        $dotPosition = strpos($number, '.');
        if ($dotPosition === false) {
            return 0;
        }
        return strlen($number) - strpos($number, '.') - 1;
    }
   
    var_dump(getBcRound('3', 0) == number_format('3', 0));
    var_dump(getBcRound('3.4', 0) == number_format('3.4', 0));
    var_dump(getBcRound('3.56', 0) == number_format('3.6', 0));
    var_dump(getBcRound('1.95583', 2) == number_format('1.95583', 2));
    var_dump(getBcRound('5.045', 2) == number_format('5.045', 2));
    var_dump(getBcRound('5.055', 2) == number_format('5.055', 2));
    var_dump(getBcRound('9.999', 2) == number_format('9.999', 2));
    var_dump(getBcRound('5.0445', 5) == number_format('5.044500', 5));
    var_dump(getBcRound('5.0445', 4) == number_format('5.04450', 4));
    var_dump(getBcRound('5.0445', 3) == number_format('5.0445', 3));
    var_dump(getBcRound('5.0445', 2) == number_format('5.045', 2));
    var_dump(getBcRound('5.0445', 1) == number_format('5.05', 1));
    var_dump(getBcRound('5.0445', 0) == number_format('5.0', 0));//
    var_dump(getBcRound('5.04455', 2) == number_format('5.045', 2));
    var_dump(getBcRound('99.999', 2) == number_format('100.000', 2));
    var_dump(getBcRound('99.999') == number_format('99.999', 0));
    var_dump(getBcRound('99.999', 'a') == number_format('99.999', 0));
    var_dump(getBcRound('99.999', -1.5) == number_format('99.999', 0));
    var_dump(getBcRound('-0.00001', 2) == number_format('-0.000', 2));
    var_dump(getBcRound('-0.0000', 2) == number_format('0', 2));
    var_dump(getBcRound('-4.44455', 2) == number_format('-4.445', 2));
    var_dump(getBcRound('-4.44555', 0) == number_format('-4.5', 0));
    var_dump(getBcRound('-4.444444444444444444444444444444444444444444445', 0) == number_format('-4.5', 0));
add a note

官方地址:https://www.php.net/manual/en/book.bc.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net