递增小数点,浮点数

编程入门 行业动态 更新时间:2024-10-28 14:25:39
本文介绍了递增小数点,浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 任何人都可以解释增加浮动,十进制数字的最佳逻辑/方法?

例如,如果我们假设开始十进制数是2.0,所以下一个项目应该自动获得号码相应地说,2.01,2.02,2.03,.....,2.09,2.10,2.11 ..等等。但是起始号码是多少,那么接下来的顺序是什么:

是吗? 2.11,2.12,2.13,2.14,..... 2.19,2.20等...? 我是逻辑正确的吗?我很困惑。请帮忙。

解决方案

完全取决于应用程序的需求。你需要数百位数吗?如果是这样,那么这些序列是有意义的。

为避免舍入错误,您可能需要存储初始值,增量数量和增量大小,如:

float start = 2.0; 浮动增量= 0.01; (int i = 0; i <10; i ++) { printf(%f,start + increment * i); }

Can anyone explain the best logic / method for incrementing floating, decimal numbers ?

For example, if we assume starting decimal number is 2.0, so the next items should automatically get number accordingly say, 2.01, 2.02, 2.03, ....., 2.09, 2.10, 2.11.. etc.

But what is the starting number is 2.1, so what would be the next sequence :

Is it ? 2.11, 2.12, 2.13, 2.14, ..... 2.19, 2.20 etc...?

I is logically correct? I am confused. Please help.

解决方案

It depends entirely on what the application needs. Do you need to count by hundredths? If so, then those sequences make sense.

To avoid round-off error, you may want to store the initial value, the number of increments, and the increment size, as in:

float start = 2.0; float increment = 0.01; for (int i = 0; i < 10; i++) { printf("%f ", start + increment * i); }

更多推荐

递增小数点,浮点数

本文发布于:2023-11-10 06:53:21,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点   浮点数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!