正文 3087字数 244,695阅读


bccomp比较二个高精确度数字
语法: int bccomp(string left operand, string right operand, int [scale]);
此函数比较二个高精确度的数字。输入二个字符串,若二个字符串一样大则返回 0;
若左边的数字字符串 (left operand) 比右边 (right operand) 的大则返回 +1;若左边的数字字符串比右边的小则返回 -1
scale 是一个可有可无的选项,表示返回值的小数点后所需的位数

不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数

BCMath 任意精度数学

BC 数学 函数
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个任意精度数字的减法
Run code
Cut to clipboard


    GMP是The GNU MP Bignum Library,是一个开源的数学运算库,它可以用于任意精度的数学运算,包括有符号整数、有理数和浮点数。它本身并没有精度限制,只取决于机器的硬件情况

    本函数库能处理的数值范围只到长整数与倍浮点数的范围。若要处理超过上述范围的数值,要使用 bc 高精确度函数库
    本函数库定义了圆周率的常量 m_pi 值为 3.14159265358979323846

    gmp_abs — Absolute value gmp_add — Add numbers gmp_and — Bitwise AND gmp_binomial — Calculates binomial coefficient gmp_clrbit — Clear bit gmp_cmp — Compare numbers gmp_com — Calculates one's complement gmp_div_q — Divide numbers gmp_div_qr — Divide numbers and get quotient and remainder gmp_div_r — Remainder of the division of numbers gmp_div — 别名 gmp_div_q gmp_divexact — Exact division of numbers gmp_export — Export to a binary string gmp_fact — Factorial gmp_gcd — Calculate GCD gmp_gcdext — Calculate GCD and multipliers gmp_hamdist — Hamming distance gmp_import — Import from a binary string gmp_init — Create GMP number gmp_intval — Convert GMP number to integer gmp_invert — Inverse by modulo gmp_jacobi — Jacobi symbol gmp_kronecker — Kronecker symbol gmp_lcm — Calculate GCD gmp_legendre — Legendre symbol gmp_mod — Modulo operation gmp_mul — Multiply numbers gmp_neg — Negate number gmp_nextprime — Find next prime number gmp_or — Bitwise OR gmp_perfect_power — Perfect power check gmp_perfect_square — Perfect square check gmp_popcount — Population count gmp_pow — Raise number into power gmp_powm — Raise number into power with modulo gmp_prob_prime — Check if number is "probably prime" gmp_random_bits — Random number gmp_random_range — Random number gmp_random_seed — Sets the RNG seed gmp_random — Random number gmp_root — Take the integer part of nth root gmp_rootrem — Take the integer part and remainder of nth root gmp_scan0 — Scan for 0 gmp_scan1 — Scan for 1 gmp_setbit — Set bit gmp_sign — Sign of number gmp_sqrt — Calculate square root gmp_sqrtrem — Square root with remainder gmp_strval — Convert GMP number to string gmp_sub — Subtract numbers gmp_testbit — Tests if a bit is set gmp_xor — Bitwise XOR
    Run code
    Cut to clipboard


      Example #1 gmp_add() example <?php $sum = gmp_add("123456789012345", "76543210987655"); echo gmp_strval($sum) . "\n"; ?> 以上例程会输出: 200000000000000
      Run code
      Cut to clipboard


        Example #1 gmp_sub() example <?php $sub = gmp_sub("281474976710656", "4294967296"); // 2^48 - 2^32 echo gmp_strval($sub) . "\n"; ?> 以上例程会输出: 281470681743360
        Run code
        Cut to clipboard