English 中文(简体)
为什么在达到预期价值之前停止这样做?
原标题:Why does this for loop stop before reaching the intended value?
for($i=0;$i<=2;$i+=0.1){
    echo  $i."<br>";
}

我的结局是:

0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2

相反的是,休息时间达到1.9<>>>>>>。 为什么?

最佳回答

由于浮动点的精确性,这一工作没有价值。 这些数字储存在基2格式中,从来不是由于四舍五入造成的。 当你增加1至1.9时,你最终不超过2.0。 你们最后说的是1.999。 接下来,根据基2格式的轮值,你最终会收到2099998美元这样的东西。

For more information see Floating point numbers and Double-precision floating-point format

你也可以做这样的事情,以取得你在之后取得的成果。

for ($i = 0; $i < 2.1; $i += .1){
    echo  $i .  <br /> ;
}
问题回答

因此,它永远不会达到<代码>floating point=integer

You can do by:

for($i=0;$i<=20;$i+=1){
    echo  ($i/10)."<br>";
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签