-5
#include<stdio.h>
void main()
{
    int i;
    float x=0.8;
    for(i=0; i<100; i++)
    x=x*(1-x)*5;
    printf("%f \n",x);
}

According to me, it looks quite simple with answer 0.80000000 but the real answer is something else. Its getting run time error with the output value "-inf". Why?

Matt S
  • 14,976
  • 6
  • 57
  • 76
vishal_ag
  • 43
  • 4
  • 4
    Why do you think it should be `0.8`? – Eugene Sh. Apr 19 '17 at 19:03
  • I'd say that it converges to infinity since you're performing a square 100 times. not really surprising. – Jean-François Fabre Apr 19 '17 at 19:05
  • 1
    I suggest you put the `printf` inside the loop then you will see the value "escape" at the 23rd iteration. This kind of investigative approach will pay off ... infinitely on the road ahead. – Weather Vane Apr 19 '17 at 19:11
  • I think it should be 0.8 because for every iteration, I am getting the value of x as 0.8. and if you look at the number(every time), its not growing. Also if you run this loop for 5 times only, still it is showing run time error with the output value not equal to 0.8. – vishal_ag Apr 19 '17 at 19:15
  • 2
    You are accumulating errors in representation. Please read [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) The value `0.8` cannot be exactly represented by `float`. – Weather Vane Apr 19 '17 at 19:16
  • so #weather Vanes, I got your point but tell me one thing, if I am doing any basic operation with the no. 0.8, system is not storing the value same all the time. Sometimes its taking 0.799996 and sometime 0.800103. But if I print this floating point value without any operation (simply I am printing x in my code) 100 or 1000 times then its value is never changing. Every time it is giving output 0.8 only. Why? – vishal_ag Apr 19 '17 at 19:37
  • `printf` rounds the value it is given, and is using the default 6 decimal places, and so the error in encoding `0.8` is not noticeable when printed; and that error is repeatable, not random. I understand why you expect `x = 0.8 * 0.2 * 5.0` to be `0.8`. But when you make further calculations, the processor uses the inaccurate representation, does not work on exactly `0.8` so the error gets increasingly worse until the value finally shoots off to infinity. BTW if you want to flag a commenter the style is @WeatherVane. – Weather Vane Apr 19 '17 at 20:09
  • 3
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Imanuel Apr 19 '17 at 21:19

2 Answers2

3

It means minus infinity.

EEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:

This is output of you're code

0.800000 
0.800000 
0.800000 
0.800001 
0.799996 
0.800011 
0.799966 
0.800103 
0.799692 
0.800923 
0.797227 
0.808280 
0.774816 
0.872380 
0.556664 
1.233946 
-1.443383 
-17.633698 
-1642.905029 
-13503899.000000 
-911776526893056.000000 
-4156682286578109086379962007552.000000 
-inf 
after this -inf -inf

because the value is out of range of flat

Pablo Escobar
  • 393
  • 2
  • 16
  • okey. I am not going to the values now. If I iterate this code for 5 times only, still it is showing run time error. why? [link] (https://ideone.com/shXVur) – vishal_ag Apr 19 '17 at 19:26
  • @vishal_ag the code works fine and output is coming correctly, this is error given by online IDEs so ignore. Try to run code in your system. – Pablo Escobar Apr 19 '17 at 19:33
2

floating point error: try taking the loop from 0 to 24 and see the output, You get around 0.800018 , that changes the 1-x part which becomes a negative answer (around 40sh loop) so at 100th it is a very low negative number that it is basically -inf.

It basically happens because x=0.8; is not 0.8 sharp but a very close number to 0.8, with loops the number grows until it affects the calculations.

There are lots of solutions -one way is to round x every time-, just search google :)

Ali
  • 406
  • 5
  • 15