Amazon Interview Question
Software Engineer in TestsI got almost the same but 16777222 (C# VS2010).
Results depend on how float numbers stored in the memory, but I don't know how exactly explain this. For double type you'll get infinite loop.
Floating point numbers are stored in base 2 "scientific notation". Once x gets large enough (about 2**24, since floats have 23 bits of significand), x is the closest float to the real number x + 1. The exact behavior depends on rounding modes and how floating point is implemented internally.
Doubles have the same problem, except you need to have the patience to get to about 2**53...
Do u mean the values achieved their max thats y they didn't increase??
as per IEEE the float representation is:
bit 31-->Sign bit
bit 30-23-->Exponent
bit 22-00--> Mantissa
In my case Exponent is 11000 and mantissa is 1. No overflow.
Not the max; the exponent could be larger. However, with respect to that exponent, the
"ones" bit of the significand (formerly the mantissa) is one past the end and thus is thrown away.
At first I thought its an infinite loop. But when I ran this I came to know that this prog will stop. These are the last two values of F1 and F2.
- Tulley February 19, 2011F1=16777216.000000, F2=16777215.000000
F1=16777216.000000, F2=16777216.000000
Can anybody explain y F1 didn't increase after 16777216.000000 ?