Pranav Jituri
Pranav Jituri

Reputation: 823

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. I tried printing the value of i and it gave me a result of 1.100000 . So I do not understand why the value is not being printed?

Upvotes: 3

Views: 556

Answers (4)

haccks
haccks

Reputation: 105992

Comparing floating point numbers: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. Different compilers and CPU architectures store temporary results at different precision, so results will differ depending on the details of your environment. 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.

In short:
1.1 can't be represented exactly in binary floating pint number. This is like the decimal representation of 10/3 in decimal which is 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

Upvotes: 2

Pascal Cuoq
Pascal Cuoq

Reputation: 80255

The quantity 11/10 cannot be represented exactly in binary floating-point, and it has different approximations as double and as float.

The constant 1.1 in the source code is the double approximation of 11/10. Since i is of type float, it ends up containing the float approximation of 1.1.

Write while (i==1.1f) or declare i as double and your program will work.

Upvotes: 5

Eric Postpischil
Eric Postpischil

Reputation: 222302

In most C implementations, using IEEE-754 binary floating-point, what happens in your program is:

  • The source text 1.1 is converted to a double. Since binary floating-point does not represent this value exactly, the result is the nearest representable value, 1.100000000000000088817841970012523233890533447265625.
  • The definition float i=1.1; converts the value to float. Since float has less precision than double, the result is 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. Since they are unequal, the result is false.

Upvotes: 12

whatsisname
whatsisname

Reputation: 6140

It is because i is not quite exactly 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.

Read: https://softwareengineering.stackexchange.com/questions/101163/what-causes-floating-point-rounding-errors

Upvotes: 1

Related Questions