=="采用浮点变量作为循环计数器和非&QUOT他们的分数递增/递减的风险;条件? [英] Any risk of using float variables as loop counters and their fractional increment/decrement for non "==" conditions?

查看:174
本文介绍了=="采用浮点变量作为循环计数器和非&QUOT他们的分数递增/递减的风险;条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是否可以安全使用浮点数作为循环计数器和递增/在每个迭代通过分级金额递减它们,就像在下面的看似无风险的项目?我当然知道,使用花车作为操作数的 == 运营商是do.But有什么错用浮筒作为操作数为正常的目的,其他的比较操作一个愚蠢的事情吗?通过正常我的意思是,好吧,即使彩车可能不会在精确数值再$ P $数量psentation,而不是像的变化0.000000001 无关,并且可以在大多数情况下被忽略? (例如,在下面的程序,它是不连表观的)

但是,说,这里是我的AP prehension.Suppose重新presentation不准确和 5.0 实际上 4.999999 。所以,我们参加由递减的 0.5 是在每次迭代中,与上次相比 0 可能变成假,循环可能要相差退出,由于 0.000001 ,将不被显示和电流输出的最后一行。我希望你得到我的错drift.How我是谁?

 #包括LT&;&stdio.h中GT;INT主要(无效)
{
浮F;为(F = 5.0 f是氟烃基; = 0; F- = 0.5)
的printf(%F \\ N,F);
}

输出:

  5.000000
4.500000
4.000000
3.500000
3.000000
2.500000
2.000000
1.500000
1.000000
0.500000
0.000000


解决方案

没有,这不是安全的,对于您的问题非常的理由。试想一下:

 #包括LT&;&stdio.h中GT;诠释主要(无效){
  浮动F = 1.0;  为(f是氟烃基; 0; F- = 0.1)
     的printf(%F \\ N,F);
  返回0;
}

这个例子似乎工作相当确定时 ˚F初始化通过 1.0 。但更改为3.0 - 事情开始很快就会方式更有趣 pretty:

  2.600000
2.500000
2.400001
...
0.000001

...导致臭名昭著的断接一个的失败。


您认为你可能是安全与> = ,而不是> ?想想一次

 浮动F = 5.0;
为(f是氟烃基; = 1; F- = 0.4)
  的printf(%F \\ N,F);...
3.400000
3.000000
2.599999
2.199999
1.799999
1.399999

...和关闭接一个,我们又来了(如 0.99999 小于1)。

Are we safe to use floats as loop-counters and to increment/decrement them by fractional amounts at each iteration,like in the seemingly risk-free program below?Of course I know that using floats as operands for the == operator is a dumb thing to do.But what's wrong with using floats as operands for other comparison operations for "normal" purposes? By "normal" I mean that,well,even though floats may not be the exact numerical representation of the number,but isn't a variation like 0.000000001 irrelevant and can be ignored in most cases? (For example in the following program that isn't even apparent)

But that said, here is my apprehension.Suppose the representation isn't exact and 5.0 is actually 4.999999.So as we go on decrementing by 0.5 at each iteration,the last comparison with 0 may turn out false and the loop may exit due to a difference of 0.000001,and the last line of current output will not be displayed. I hope you are getting my drift.How wrong am I?

#include<stdio.h>

int main(void)
{
float f;

for(f=5.0;f>=0;f-=0.5)
printf("%f\n",f);
}

Output:

5.000000
4.500000
4.000000
3.500000
3.000000
2.500000
2.000000
1.500000
1.000000
0.500000
0.000000

解决方案

No, it's not safe, for the reasons given in your very question. Consider this:

#include<stdio.h>

int main(void) {
  float f = 1.0;

  for(;f>0;f-=0.1)
     printf("%f\n",f);
  return 0;
}

This example seems to work quite ok when f is initialized by 1.0. But change this to 3.0 - and things start to get way more interesting pretty soon:

2.600000
2.500000
2.400001
...
0.000001

... leading to the infamous 'off-by-one' failure.


You think that you might be safe with >= instead of >? Think again:

float f = 5.0;
for(;f>=1;f-=0.4)
  printf("%f\n",f);

...
3.400000
3.000000
2.599999
2.199999
1.799999
1.399999

... and off-by-one we go again (as 0.99999 is less than 1).

这篇关于==&QUOT;采用浮点变量作为循环计数器和非&QUOT他们的分数递增/递减的风险;条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆