为什么打印最后一个数字(1)? [英] Why is the last number (1) printed?
问题描述
<?php
$ start = 0;
$ stop = 1;
$ step =($ stop - $ start)/ 10;
$ i = $ start + $ step;
while($ i <$ stop){
echo($ i。< br />);
$ i + = $ step;
}
?>
输出:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1< - 注意1不应该打印
创建一个小提琴
另外:如果你设置 $ start = 1
和 $ stop = 2
工作正常。
使用: php 5.3.27
是 1
打印了吗?
,有时它的代表性也有缺陷 - 情况就是如此这里。
您实际上并没有得到0.1,0.2,... - 这很容易检查:
$ start = 0;
替换为
$ stop = 1;
$ step =($ stop - $ start)/ 10;
$ i = $ start + $ step;
while($ i <$ stop){
print(number_format($ i,32)。< br />);
$ i + = $ step;
$ b $ p
$ b $ p $这里唯一的区别就是你看到的是code > echonumber_format
调用。但结果却大相径庭:
0.10000000000000000555111512312578
0.20000000000000001110223024625157
0.30000000000000004440892098500626
0.40000000000000002220446049250313
0.50000000000000000000000000000000
0.59999999999999997779553950749687
0.69999999999999995559107901499374
0.79999999999999993338661852249061
0.89999999999999991118215802998748
0.99999999999999988897769753748435
看?实际上只有一次是
0.5
- 因为这个数字可以存储在一个浮点容器中。所有其他人只是近似。
如何解决这个问题?那么,一个激进的方法是使用不漂浮,但在整体类似的情况。很容易注意到,你是这样做的...
$ start = 0;
$ stop = 10;
$ step =(int)(($ stop - $ start)/ 10);
$ i = $ start + $ step;
while($ i <$ stop){
print(number_format($ i,32)。< br />);
$ i + = $ step;
$ / code $ / pre
...它可以正常工作:
或者,您可以使用
number_format
将float转换为某个字符串,然后将此字符串与预格式化的float进行比较。像这样:$ start = 0;
$ stop = 1;
$ step =($ stop - $ start)/ 10;
$ i = $ start + $ step;
while(number_format($ i,1)!== number_format($ stop,1)){
print(number_format($ i,32)。\\\
);
$ i + = $ step;
}
The code:
<?php $start = 0; $stop = 1; $step = ($stop - $start)/10; $i = $start + $step; while ($i < $stop) { echo($i . "<br/>"); $i += $step; } ?>
The output:
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 <-- notice the 1 printed when it shouldn't
Created a fiddle
One more: if you set
$start = 1
and$stop = 2
it works fine.Using:
php 5.3.27
Why is the
1
printed?解决方案Because not only float math is flawed, sometimes its representation is flawed too - and that's the case here.
You don't actually get 0.1, 0.2, ... - and that's quite easy to check:
$start = 0; $stop = 1; $step = ($stop - $start)/10; $i = $start + $step; while ($i < $stop) { print(number_format($i, 32) . "<br />"); $i += $step; }
The only difference here, as you see, is that
echo
replaced withnumber_format
call. But the results are drastically different:0.10000000000000000555111512312578 0.20000000000000001110223024625157 0.30000000000000004440892098500626 0.40000000000000002220446049250313 0.50000000000000000000000000000000 0.59999999999999997779553950749687 0.69999999999999995559107901499374 0.79999999999999993338661852249061 0.89999999999999991118215802998748 0.99999999999999988897769753748435
See? Only one time it was
0.5
actually - because that number can be stored in a float container. All the others were only approximations.How to solve this? Well, one radical approach is using not floats, but integers in similar situations. It's easy to notice that have you done it this way...
$start = 0; $stop = 10; $step = (int)(($stop - $start) / 10); $i = $start + $step; while ($i < $stop) { print(number_format($i, 32) . "<br />"); $i += $step; }
... it would work ok:
Alternatively, you can use
number_format
to convert the float into some string, then compare this string with preformatted float. Like this:$start = 0; $stop = 1; $step = ($stop - $start) / 10; $i = $start + $step; while (number_format($i, 1) !== number_format($stop, 1)) { print(number_format($i, 32) . "\n"); $i += $step; }
这篇关于为什么打印最后一个数字(1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!