比较for循环中的double [英] Comparing double in for loop
问题描述
大家好,
我遇到了最奇怪的行为。看看这个简单的
程序:
#include< stdio.h>
int main()
{
双倍时间= 1;
双倍i = 0;
for(double i = 0; i< time; i + = 0.01)
{
if(i == 0.15)
{
printf("找到%f \ n",i);
}
if(i< 0.1)
{
printf(" foundXX%f\ n,i);
}
}
>
返回1;
}
您希望打印什么:
0.0的所有数字使用prefex来修改0.9:foundXX
和输出中的最后一行应该找到0.15 - 对吧?!
错误...我得到的是从0.0到0.0的所有数字0.1打印
(包括0.1 !!)
检查是否(i == 0.1){printf(" foundXX%f \ n,i);}它没有
print foundXX 0.1 !!
为什么它认为0.1是<比0.1 !! ??
任何人?
谢谢
Hi all,
i have encountered the strangest behavior. Check out this simple
program:
#include <stdio.h>
int main()
{
double time = 1;
double i = 0;
for (double i = 0; i < time ; i+=0.01 )
{
if ( i == 0.15 )
{
printf( "found %f\n", i);
}
if ( i < 0.1 )
{
printf( "foundXX %f\n", i);
}
}
return 1;
}
What would you expect to be printed:
All the numbers from 0.0 to 0.9 with the prefex: foundXX
and last line in output should be found 0.15 - right?!
Wrong... what I get is all the numbers from 0.0 to 0.1 printed
(including 0.1!!)
When checking if ( i==0.1) { printf( "foundXX %f\n",i);} it does not
print foundXX 0.1!!
Why exactly does it think that 0.1 is < than 0.1!!??
anyone?
Thanks
推荐答案
em ************ @ gmail.com 写道:
em************@gmail.com writes:
>大家好,
>Hi all,
>我遇到了最奇怪的行为。
>i have encountered the strangest behavior.
参见
http://www.eason.com/library/math/floatingmath.pdf
或(更容易,更适合您的问题)
http://www.mathworks.com/company/new。 ..all96Cleve.pdf
Tim Love写道:
Tim Love wrote:
http://www.eason.com/library/math/floatingmath.pdf
或(更容易,更适合您的问题)
http://www.mathworks.com/company/new...all96Cleve.pdf
阅读材料很好。快速回答:
http://www.parashift.com/c++-faq-lit...html#faq-29.17
em ************ @ gmail.com 写道:
for(double i = 0; i< time; i + = 0.01)
{
if(i == 0.15)
{
printf(" found%f\ n,i);
}
if (i <0.1)
{
printf(" foundXX%f\ n,i);
}
}
for (double i = 0; i < time ; i+=0.01 )
{
if ( i == 0.15 )
{
printf( "found %f\n", i);
}
if ( i < 0.1 )
{
printf( "foundXX %f\n", i);
}
}
正如其他人所指出的那样,0.01不能用
浮点数准确表示(出于同样的原因)例如,1/3不能用十进制数字准确表示
。
显然你想要循环的精确迭代次数,并且y ou
想要精确控制
循环计数器的精确值。在这些情况下,你应该做的是使用一个整数
循环计数器,并从中计算浮点值。在其他
字:
for(int i = 0; i< 100; ++ i)
{
double value = i / 100.0;
if(i == 15)std :: cout<< 找到 <<值<< " \ n";
if(i< 10)std :: cout<< foundXX <<值<< " \ n";
}
整数循环计数器将确保精确的
迭代次数为执行,并在
条件中比较这个整数循环计数器将确保这些条件给出准确的
结果。每当需要使用浮点值时,请使用
''value''变量,如上所示。
As others have pointed out, 0.01 cannot be represented accurately with
floating point numbers (for the same reason as eg. 1/3 cannot be
represented accurately with decimal numbers).
Clearly you want a precise number of iterations to your loop, and you
want precise control on what happens with some precise values of the
loop counter. In those cases what you should do is to use an integer
loop counter, and calculate the floating point value from it. In other
words:
for(int i = 0; i < 100; ++i)
{
double value = i/100.0;
if(i == 15) std::cout << "found " << value << "\n";
if(i < 10) std::cout << "foundXX " << value << "\n";
}
The integer loop counter will make sure that an accurate number of
iterations is performed, and comparing this integer loop counter in the
conditionals will make sure that those conditionals give an accurate
result. Whenever the floating-point value needs to be used, use the
''value'' variable, as exemplified above.
这篇关于比较for循环中的double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!