使用float / double作为循环变量 [英] Using float / double as a loop variable

查看:1093
本文介绍了使用float / double作为循环变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在x64架构上运行Visual Studio 2013 Windows 8.1



我试图运行一个使用float作为其循环变量的循环,问题是在

 增加操作的十进制值丢失。 float incrementValue = 360/14; 
for(float i = 0; i <= 360; i = i + incrementValue)
{
cout< endl<< I<一世;
}

我需要i>​​ = 360的值。但它停在350。
由于360/14 = 25.7142857
,但看起来增量是以25为步长。
如果我做任何整数的同样的事情,它的工作正常,这个问题只有任何数字的形式xx.yyyy



我试过查找

解决方案

这里有三个问题(两个相关的),所有这些都可能是贡献。



第一个是浮点比较通常是不明智的,虽然你可以您有 360 ,实际上可能是 359.999999942



<第二,不准确性随着时间的推移而增长。每次您添加一个 0.99 的数字,您认为
1 ,错误累积。



使用您使用的值,这些错误可能会很小,但您应该意识到它们。如果你开始处理大量的数字,你会很快找到问题。






最后的,无关的,问题是 360/14 是整数除法,它会给你一个整数结果, 25 而不是 25.714285714



您可以通过确保其中一个值为float:

  float incrementValue = float(360) 

但这不会解决前两个问题,

要修复 ,你将更好地坚持使用整数(对于这种简单的情况)和转换为浮点在最近的可能实例:

 #include< iostream> 
int main(void){
int incrementValue = 360;
for(int i = 0; i <= 360 * 14; i = i + incrementValue)
std :: cout < I< float(i)/ 14< '\\\
';
return 0;
}

这会提供:

  I 0 
I 25.7143
I 51.4286
I 77.1429
I 102.857
I 128.571
I 154.286
I 180
I 205.714
I 231.429
I 257.143
I 282.857
I 308.571
I 334.286
I 360


Running on a x64 architecture , Visual Studio 2013 Windows 8.1

I am trying to run a loop which uses a float as its loop variable , the issue is during the increment operation the decimal values are being lost .

Eg :

float incrementValue = 360 / 14;
for (float i = 0; i <= 360; i = i + incrementValue)
{
    cout << endl << " I " << i;
}

I need the value of i >= 360 . But it stops at 350 . Since 360/14 =25.7142857 but looks like the increments are in steps of 25 . If i do the same thing with any whole number it works fine , this issue is only with any number of the form xx.yyyy

I tried looking up various sites on this issue but i was unable to find anything that helps me / answers my questions .

解决方案

You have three problems here (two related), all of which are probably contributing.

The first is that floating point comparisons are generally unwise since, while you may think you have 360, it may in fact be 359.999999942.

The second is that inaccuracies build up over time. Every time you add a number like 0.99 that you think is 1, the error accumulates.

With the values you're using, these errors are likely to stay small but you should be aware of them anyway. If you start processing lots of numbers, you'll find out about the problems pretty quickly.


The final, and unrelated, problem is that 360 / 14 is integer division, and it will give you an integer result, 25 instead of 25.714285714.

You can fix that final problem by ensuring one of the values is a float:

float incrementValue = float(360) / 14;

But that's not going to fix the first two, which will bite you at some point.

To fix that, you would be better off sticking with integers (for this simple case anyway) and converting to floating point at the latest possible instance:

#include<iostream>
int main (void) {
    int incrementValue = 360;
    for (int i = 0; i <= 360 * 14; i = i + incrementValue)
        std::cout << " I " << float(i) / 14 << '\n';
    return 0;
}

which gives you:

 I 0
 I 25.7143
 I 51.4286
 I 77.1429
 I 102.857
 I 128.571
 I 154.286
 I 180
 I 205.714
 I 231.429
 I 257.143
 I 282.857
 I 308.571
 I 334.286
 I 360

这篇关于使用float / double作为循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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