栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以依靠PHP php.ini精度解决方法来解决浮点问题吗

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以依靠PHP php.ini精度解决方法来解决浮点问题吗

介绍

浮点算术被许多人视为深奥的话题。这是相当令人惊讶的,因为浮点数在计算机系统中无处不在。大多数小数没有精确表示为二进制分数,因此会进行一些舍入。一个好的开始是每位计算机科学家都应该了解的浮点运算法则

问题

问题1

如果我只需要 精确的2位数字 (货币),我可以依靠此解决方案吗?

答案1

如果您需要 精确的2位数字, 那么答案是 否定的, 即使您一直使用php精度设置也无法始终确定 2位 小数

not going towork on numbers higher than 10^6

在计算过程中,如果长度小于8,则可能会增加精度长度

问题2

如果不能,当此解决方案失败时,您能否提供清晰的示例?

答案2

ini_set('precision', 8); // your precision$a =  5.88 ; // cost of 1kg$q = 2.49 ;// User buys 2.49 kg$b = $a * 0.01 ; // 10% Discount only on first kg ;echo ($a * $q) - $b;

输出量

14.5824 <---- not precise 2 digits calculations even if precision is 8

问题3

哪个php.ini.precision值最适合两位数字和金钱计算?

答案3

精度和金钱计算是两件不同的事情…使用PHP精度作为财务计算或浮点长度的基础不是一个好主意

简单测试

为了避免运行一些例子使用起来

bcmath
number_format
并简单
minus

base

$a = 342349.23;$b = 341765.07;

Example A

ini_set('precision', 20); // set to 20 echo $a - $b, PHP_EOL;echo floatval(round($a - $b, 2)), PHP_EOL;echo number_format($a - $b, 2), PHP_EOL;echo bcsub($a, $b, 2), PHP_EOL;

输出量

584.15999999997438863584.15999999999996817    <----- Round having a party 584.16584.15  <-------- here is 15 because precision value is 20

Example B

ini_set('precision', 14); // change to  14 echo $a - $b, PHP_EOL;echo floatval(round($a - $b, 2)), PHP_EOL;echo number_format($a - $b, 2), PHP_EOL;echo bcsub($a, $b, 2), PHP_EOL;

输出量

584.15999999997584.16584.16584.16  <-------- at 14 it changed to 16

Example C

ini_set('precision', 6); // change to  6 echo $a - $b, PHP_EOL;echo floatval(round($a - $b, 2)), PHP_EOL;echo number_format($a - $b, 2), PHP_EOL;echo bcsub($a, $b, 2), PHP_EOL;

输出量

584.16584.16584.16584.00  <--- at 6 it changed to 00

Example D

ini_set('precision', 3); // change to 3echo $a - $b, PHP_EOL;echo floatval(round($a - $b, 2)), PHP_EOL;echo number_format($a - $b, 2), PHP_EOL;echo bcsub($a, $b, 2), PHP_EOL;

输出量

584584584.16   <-------------------------------- They only consistent value 0.00  <--- at 3 .. everything is gone

结论

忘掉浮点数,

cents
然后算入,然后除以
100
是否太晚,只要简单地使用
number_format
它就对我来说是一致的。

更新资料

问题1:对于0..999999.99之间的数字(其中A和B是带小数位的数字),精度解决方法会失败吗?如果是这样,请提供一个例子

0
999999.99
递增的形式
0.01
大约
99,999,999
是循环的组合可能性,
9,999,999,800,000,000

我真的不认为有人愿意为您运行这种测试。

由于浮点数是具有有限精度的二进制数,因此尝试设置

precision
它对确保精度的作用有限。这是一个简单的测试:

ini_set('precision', 8);$a = 0.19;$b = 0.16;$c = 0.01;$d = 0.01;$e = 0.01;$f = 0.01;$g = 0.01;$h = $a + $b + $c + $d + $e + $f + $g;echo "Total: " , $h , PHP_EOL;$i = $h-$a;$i = $i-$b;$i = $i-$c;$i = $i-$d;$i = $i-$e;$i = $i-$f;$i = $i-$g;echo $i , PHP_EOL;

输出量

Total: 0.41.0408341E-17     <--- am sure you would expect 0.00 here ;

尝试

echo round($i,2) , PHP_EOL;echo number_format($i,2) , PHP_EOL;

输出量

00.00    <------ still confirms number_format is most accurate to maintain 2 digit


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号