• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> php 外部扩展库

    gmp库(多精度计算)

    GMP(The GNU Multiple Precision Arithmetic Library)又叫GNU多精度算术库,是一个提供了很多操作高精度的大整数,浮点数的运算的算术库,几乎没有什么精度方面的限制,功能丰富。我刚接触到这个东西的时候是在学习PHP的过程中。GMP的主要目标应用领域是密码学的应用和研究、 互联网安全应用、 代数系统、 计算代数研究等。

    在PHP 5.6之前,大多数GMP函数操作或返回GMP编号资源。PHP 5.6以后的版本使用GMP对象代替GMP编号资源。

    安装

    为了使这些函数可用,必须使用--with GMP选项.在GMP支持下编译PHP。

    on Ubuntu Server:
    sudo apt-get install libgmp-dev
    sudo apt-get install php5-gmp
    sudo service apache2 reload
    
    Under Windows you will need to un-comment the line in your php.ini file
    ;extension=gmp
    within the php.ini and restart Apache.
    
    On ArchLinux php 5.4.14-1 I had to uncomment the line
    extension=gmp.so
    in /etc/php/php.ini file.
    Probably, restart of web server (e.g. Apache) is required if working from web.
    

    例子

    <?php
    function fact($x) 
    {
        $return = 1;
        for ($i=2; $i <= $x; $i++)
        {
            $return = gmp_mul($return, $i);
        }
        return $return;
    }
    echo gmp_strval(fact(1000)) . "\n";
    ?>