0
#include<stdio.h>
#include<conio.h>
main()
{
  float i=0.0;
  while(i!=1.0)
  {
      i+=0.1;
      printf("%f",i);
  }
  getch(
}

why its output is infinite? Can anyone tell what should be done to make this finite ?

  • Try `while (i < 1.0)`. Floating point numbers are not precise... darn base 2 system! – embedded_guy Jun 07 '14 at 04:26
  • @embedded_guy - It is not down to the base – Ed Heal Jun 07 '14 at 04:30
  • 6
    Get some munchies and a drink, [**then read this**.](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) It is a little daunting, but you'll remember it the rest of your career. – WhozCraig Jun 07 '14 at 04:32
  • 1
    please to a search. there are hundreds of duplicate about this on SO http://stackoverflow.com/questions/6874357/why-0-1-0-2-0-3-in-d http://stackoverflow.com/questions/16588985/c-c-floating-point-issue http://stackoverflow.com/questions/884538/printing-increments-of-0-1-in-c-sharp – phuclv Jun 07 '14 at 05:05
  • @embedded_guy it's just that it can't represent lots of numbers in decimal, and it's precise in what it can represent. Speaking like that, decimal is not precise because it cannot represent exactly an infinite number of numbers in base 60 or base 3... – phuclv Jun 07 '14 at 13:14
  • @LưuVĩnhPhúc, Yeah, it was a joke. The fact is that the OP is trying to represent a base 10 number in base 2... there's nothing wrong with base 2, it's just not base 10. – embedded_guy Jun 07 '14 at 17:28
  • @EdHeal I thought the whole problem was with trying to represent a base 10 number with a base 2 system. – embedded_guy Jun 07 '14 at 17:30

4 Answers4

2

In the cases where you want to iterate over floating point numbers you should consider to iterate over integers and divide by floating point numbers.

In your case:

#include<stdio.h>
main()
{
  int i=0;
  while(i!=10)
  {
      i+=1;
      printf("%f",i/10.0f);
  }
}

That way you get the best approximation and no accumulating rounding errors due to adding floating point numbers.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
2

The loop is infinite because under a computer's floating-point arithmetic, adding 0.1 ten times is not equal to 1.

This is because computers use binary numbers to do arithmetic, and the number 0.1 cannot be expressed as a terminating binary number. So the computer has to pick a binary number that does terminate and that fits inside a float as an approximation to 0.1. Unfortunately, it is imprecise enough that adding ten copies of this approximation together does not create a float that's equal to 1.

If you change the format string in your loop to show a much higher precision, you can see that the values of i in the loop are not exact:

0.10000000149011611938
0.20000000298023223877
0.30000001192092895508
0.40000000596046447754
0.50000000000000000000
0.60000002384185791016
0.70000004768371582031
0.80000007152557373047
0.90000009536743164062
1.00000011920928955078

Generally, you should not compare floating-point numbers for equality. You can use inequality comparisons, check if two numbers are almost equal by whether their difference is smaller than some small fixed value of epsilon, or use integers for iteration and only convert to floating point when you need it. The other answers already explain these alternatives, so I won't go into them here.

betaveros
  • 1,360
  • 12
  • 23
0

Floating point numbers are not precise.

Therefore stuff like equality have long ago flown out of the window.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

You cannot compare floating point numbers like that because they are not precise. You can check within a range though.

#define epsilon 0.000005

main()
{
  float i=0.0;
  while(!(i > 1.0 - epsilon && i < 1.0 + epsilon))
  {
      i+=0.1;
      printf("%f",i);
  }
  getch();
}
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33