<?php
$f = 0.58;
var_dump(intval($f * 100)); //为啥输出57
为啥输出是57啊? PHP的bug么?
我相信有很多的同学有过这样的疑问, 因为光问我类似问题的人就很多, 更不用说bugs.php.net上经常有人问…
要搞明白这个原因, 首先我们要知道浮点数的表示:
浮点数, 以64位的长度为例, 会采用1位符号位(E), 11指数位(Q), 52位尾数(M)表示(一共64位).
符号位:最高位表示数据的正负,0表示正数,1表示负数。
指数位:表示数据以2为底的幂,指数采用偏移码表示
尾数:表示数据小数点后的有效数字.
这里的关键点就在于, 小数在二进制的表示, 关于小数如何用二进制表示, 大家可以百度一下, 我这里就不再赘述, 我们关键的要了解, 0.58 对于二进制表示来说, 是无限长的值(下面的数字省掉了隐含的1)..
0.58的二进制表示基本上(52位)是: 0010100011110101110000101000111101011100001010001111
0.57的二进制表示基本上(52位)是: 0010001111010111000010100011110101110000101000111101
而两者的二进制, 如果只是通过这52位计算的话,分别是:
0.58 -> 0.57999999999999996
0.57 -> 0.56999999999999995
0.58 * 100 = 57.999999999
那你intval一下, 自然就是57了….
可见, 这个问题的关键点就是: “你看似有穷的小数, 在计算机的二进制表示里却是无穷的”
so, 不要再以为这是PHP的bug了, 这就是这样的…..
Actually its referred to as Search engine optimization that when i search for this post I found this web page at the top of all web pages in search engine. cdecdgdcfcdb
精彩新闻资讯,热点技术文章 欢迎来 M科技资讯
Hy there, Good news ! a fineoffering
Just click
https://drive.google.com/file/d/1SAL1agHLbzpovNk5e0G4hU9mKNNkilnq/preview
an significantoffers
Untangle click on the affiliation subordinate to to compliant
https://drive.google.com/file/d/15LdEwXzrYU-TVd8_7XULdo2jfKkmFKWI/preview
stricttender
Are you in?
https://drive.google.com/file/d/1Fom0ku9jHoh0LQfokIe6npagO5NT9w7T/preview
Высокое качество парфюмерии только у нас
about his Как зайти на hydra
One of the most skilled calligraphers
Preserved about 300 thousand.
from a printed book, reproduction
Century to a kind of destruction:
text carrier and protective
antiquities. These are the Egyptian papyri
By the end of the 15th century, 35
from a printed book, reproduction