你或许知道强制转换会丢失精度,你或许了解或多或少类似的细节,你或许也有遗漏的细节
1+1 = 2
3*(1/3) =1 没错 但是你给计算机试试
3*0.3333... =0.999999999 != 1
这就是细节,什么时候注意呢,得看情况了,比如是现金、利率时候就的注意了
<?php echo (int)((0.1+0.0)*10); //1 echo"<br>"; echo (int)((0.1+0.1)*10); //2 echo"<br>"; echo (int)((0.1+0.7)*10); //7 echo"<br>"; echo (0.1+0.7); //0.8 echo"<br>"; echo intval((0.1+0.7) * 10); //7 而不是8 echo"<br>"; echo intval(bcadd(0.1,0.7,8)*10); //8 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 仔细想下,你或许明白,但是我还是没明白,因为我笨,于是找到php。chm 给出一些解释 Warning 关于浮点数精度的警告 显然简单的十进制分数如同 0.1 或 0.7 不能在不丢失一点点精度的情况下转换为内部二进制的格式。这就会造成混乱的结果:例如,floor((0.1+0.7)*10) 通常会返回 7 而不是预期中的 8,因为该结果内部的表示其实是类似 7.9。 这和一个事实有关,那就是不可能精确的用有限位数表达某些十进制分数。例如,十进制的 1/3 变成了 0.3。 所以永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数。 至于上面中的任意精度计算可参考php.chm ◾bcadd — Add two arbitrary precision numbers string bcadd ( string $left_operand , string $right_operand [, int $scale ] )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
