美文网首页
php浮点数计算

php浮点数计算

作者: 啦啦一路高歌 | 来源:发表于2020-09-17 15:24 被阅读0次
image.png

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

左操作数和右操作数求和 ,scale 用于设置结果中小数点后的小数位数。
bcadd ( string left_operand , stringright_operand [, int $scale ] ) : string

<?php
$a = '1.234';
$b = '5';
    echo bcadd($a, $b);     // 6
    echo bcadd($a, $b, 4);  // 6.2340
?>

bccomp — 比较两个任意精度的数字
bccomp ( string left_operand , stringright_operand [, int $scale = int ] ) : int

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

bcdiv — 2个任意精度的数字除法计算
bcdiv ( string left_operand , stringright_operand [, int $scale = int ] ) : string

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

bcmod — 对一个任意精度数字取模
bcmod ( string left_operand , stringmodulus ) : string

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

bcmul — 2个任意精度数字乘法计算
bcmul ( string left_operand , stringright_operand [, int $scale = int ] ) : string

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

bcpow — 任意精度数字的乘方
bcpow ( string left_operand , stringright_operand [, int $scale ] ) : string

<?php
    echo bcpow('4.2', '3', 2); // 74.08
?>

bcpowmod----将任意的精确数提高到另一个,再用指定的模量还原
bcpowmod ( string base , stringexponent , string modulus [, intscale = 0 ] ) : string

<?php
    $a = bcpowmod($x, $y, $mod);
    $b = bcmod(bcpow($x, $y), $mod);// 
    $a and $b are equal to each other.
?>

bcscale 设置全局默认的小数位数
bcscale — 设置所有bc数学函数的默认小数点保留位数
bcscale ( int $scale ) : bool

<?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 — 任意精度数字的二次方根
bcsqrt ( string operand [, intscale ] ) : string

<?php
    echo bcsqrt('2', 3); // 1.414
?>

bcsub — 2个任意精度数字的减法
bcsub ( string left_operand , stringright_operand [, int $scale = int ] ) : string

<?php
    $a = '1.234';
    $b = '5';
    echo bcsub($a, $b);     // -3
    echo bcsub($a, $b, 4);  // -3.7660
?>

相关文章

  • php浮点数计算

    bcadd — 2个任意精度数字的加法计算 左操作数和右操作数求和 ,scale 用于设置结果中小数点后的小数位数...

  • php公式计算

    1 通过evel计算公式 注意浮点数运算 eval — 把字符串作为PHP代码执行 2 通过bc函数实现 任意精度...

  • PHP浮点数精度问题

    PHP常见的浮点数“bug” 浮点数的表示形式 浮点数的表示(IEEE 754): 浮点数, 以64位的长度(双精...

  • 22.系统的讲解 - PHP 浮点数高精度运算

    转发地址:系统的讲解 - PHP 浮点数高精度运算

  • 点滴笔记(2018年08月24日更新)

    问:浮点数为什么存在精度问题? 计算机中的浮点数对应于数学当中的小数。简单计算下,32位浮点数最多可以表示2^32...

  • 笨办法学Python ex03

    数字和数学计算 输入: 运行: 附加题 浮点数(floating point number) “浮点数”的定义是相...

  • PHP 日期加减计算方法示例

    PHP 标准的日期格式 PHP 简单的日期加减计算 运行结果: PHP 进阶的日期加减计算 运行结果: PHP 高...

  • 浮点数计算

    偶尔在一些git代码中看到一些关于浮点数运算的代码,拿过来备用之后可以用下.

  • 浮点数计算

    function accAdd(arg1, arg2) { var r1, r2, m; try { r1...

  • php浮点数的精度

    php浮点数的精度 Q:php商城项目中使用 floor()向下取整,出现问题,floor(580.8/110*1...

网友评论

      本文标题:php浮点数计算

      本文链接:https://www.haomeiwen.com/subject/otwlyktx.html