1

(I ask this on math.se but was advised to come over here... Go easy on me, I am not a CS person.)

I am learning to program and came across the interesting byproduct of computer number representation shown by

0.1 + 0.2 = 0.30000000000000004

In trying to understand why this occurs, one has to understand how 0.1 is stored on the computer. I worked out that

0.1(10) = 0.00011001100...(2)

and subsequently read that the number floating-point numbers closest to 0.1 are

0.09999999999999999167332731531132594682276248931884765625

and

0.1000000000000000055511151231257827021181583404541015625

(source: https://stackoverflow.com/a/27030789).

Curious as to how to actually determine these number(s), I attempted to write a python script to find them and failed miserably.

I then turned to google and found this answer to a similar question: https://math.stackexchange.com/a/2119191/22276. I was able to follow the solution completely in my case until the portion computing the numbers 14411518807585587 and 14411518807585588. How do I use these numbers to get the two surrounding numbers.

For reference, my two integers would be determined by:

2-4=.0625 <= 0.1 < .125 = 2-3

causing e = -4. I then compute the integer part of

0.1 * 253-(-4) which is

144 115 188 075 855 872

This is where I am stuck. Could someone walk me through this with the impression your audience is CS-illiterate?

Community
  • 1
  • 1
floater
  • 23
  • 2
  • I'm having trouble understanding what your question is here. There are two approximations: Only finitely many floating point numbers are represented exactly, and those don't generally correspond to terminating decimal representations. The arithmetic is also only approximate. But you seem to know that already.... – Brick Sep 16 '17 at 01:51
  • @Brick: My question is essentially: Suppose x is a terminating base-10 rational in the range 0 – floater Sep 16 '17 at 02:37
  • The number you computed 144 115 188 075 855 872 is the decimal representation of the mantissa for the lower bound. Add 1 for the decimal representation of the mantissa for the upper bound. Using the nomenclature in the Math.SE question. Probably there is some corner case where you're just at the break between changing e by an integer, but that's a relatively small set that you might spot (?) at the point where you computed e in the first place. – Brick Sep 16 '17 at 03:48

0 Answers0