简体   繁体   English

没有输出进入简单的C程序

[英]No Output Coming In Simple C Program

I have been asked a very simple question in the book to write the output of the following program - 我在本书中被问到一个非常简单的问题来编写以下程序的输出 -

#include<stdio.h>

int main()
{
   float i=1.1;
   while(i==1.1)
      {
         printf("%f\n",i);
         i=i-0.1;
      }
return 0;
}

Now I already read that I can use floating point numbers as loop counters but are not advisable which I learned. 现在我已经读过我可以使用浮点数作为循环计数器但不建议我学习。 Now when I run this program inside the gcc, I get no output even though the logic is completely correct and according to which the value of I should be printed once. 现在,当我在gcc中运行这个程序时,即使逻辑完全正确并且I的值应该打印一次,我也没有输出。 I tried printing the value of i and it gave me a result of 1.100000 . 我尝试打印i的值,它给了我1.100000的结果。 So I do not understand why the value is not being printed? 所以我不明白为什么价值不被打印?

In most C implementations, using IEEE-754 binary floating-point, what happens in your program is: 在大多数C实现中,使用IEEE-754二进制浮点,程序中发生的事情是:

  • The source text 1.1 is converted to a double . 源文本1.1转换为double Since binary floating-point does not represent this value exactly, the result is the nearest representable value, 1.100000000000000088817841970012523233890533447265625. 由于二进制浮点不完全表示此值,因此结果是最接近的可表示值1.100000000000000088817841970012523233890533447265625。
  • The definition float i=1.1; 定义float i=1.1; converts the value to float . 将值转换为float Since float has less precision than double , the result is 1.10000002384185791015625. 由于float精度低于double ,因此结果为1.10000002384185791015625。

In the comparison i==1.1 , the float 1.10000002384185791015625 is converted to double (which does not change its value) and compared to 1.100000000000000088817841970012523233890533447265625. 在比较i==1.1float 1.10000002384185791015625转换为double (不改变其值)并与1.100000000000000088817841970012523233890533447265625进行比较。 Since they are unequal, the result is false. 由于它们不相等,结果是错误的。

The quantity 11/10 cannot be represented exactly in binary floating-point, and it has different approximations as double and as float . 数量11/10不能用二进制浮点精确表示,它有不同的近似值doublefloat

The constant 1.1 in the source code is the double approximation of 11/10. 源代码中的常量1.1是11/10的double近似值。 Since i is of type float , it ends up containing the float approximation of 1.1 . 由于ifloat类型,它最终包含1.1float近似值。

Write while (i==1.1f) or declare i as double and your program will work. while (i==1.1f)或声明idouble ,你的程序将工作。

Comparing floating point numbers: 1 比较浮点数: 1

Floating point math is not exact. 浮点数学并不准确。 Simple values like 0.2 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. 0.2这样的简单值无法使用二进制浮点数精确表示,浮点数的有限精度意味着操作顺序的微小变化可能会改变结果。 Different compilers and CPU architectures store temporary results at different precision, so results will differ depending on the details of your environment. 不同的编译器和CPU架构以不同的精度存储临时结果,因此结果将根据环境的详细信息而有所不同。 If you do a calculation and then compare the results against some expected value it is highly unlikely that you will get exactly the result you intended. 如果您进行计算,然后将结果与某个预期值进行比较,那么您很可能无法获得预期的结果。

In other words, if you do a calculation and then do this comparison: 换句话说,如果您进行计算然后进行此比较:

 if (result == expectedResult) 

then it is unlikely that the comparison will be true. 那么比较是不可能的。 If the comparison is true then it is probably unstable – tiny changes in the input values, compiler, or CPU may change the result and make the comparison be false. 如果比较结果为真,那么它可能不稳定 - 输入值,编译器或CPU的微小变化可能会改变结果并使比较结果为假。

In short: 简而言之:
1.1 can't be represented exactly in binary floating pint number. 1.1无法准确表示二进制浮点数。 This is like the decimal representation of 10/3 in decimal which is 3.333333333.......... 这就像十进制10/3的十进制表示,即3.333333333..........

I would suggest you to Read the article What Every Computer Scientist Should Know About Floating-Point Arithmetic . 我建议你阅读文章科学家应该知道的关于浮点算术的内容


1. For the experts who are encouraging beginner programmers to use == in floating point comparision 1.对于鼓励初学者程序员在浮点比较中使用==的专家

It is because i is not quite exactly 1.1. 这是因为我不完全是1.1。

If you are going to test a floating point, you should do something along the lines of while(i-1.1 < SOME_DELTA) where delta is the threshold where equality is good enough. 如果你要测试浮点数,你应该按照while(i-1.1 < SOME_DELTA)做一些事情,其中​​delta是相等足够好的阈值。

Read: https://softwareengineering.stackexchange.com/questions/101163/what-causes-floating-point-rounding-errors 阅读: https//softwareengineering.stackexchange.com/questions/101163/what-c​​auses-floating-point-rounding-errors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM