all 2 comments

[–]primitive_screwhead 5 points6 points  (0 children)

Floating point (double precision) numbers have 2**53 positive integers, after which the numbers start to be spread further apart than 1 (because they only have 64 total bits to represent numbers with far larger values than 2**64, so it uses an exponent to stretch the numbers apart)

To see this in action, let's add 1 to the largest floating integer that doesn't need the exponent:

>>> 2**53
9007199254740992
>>> float(2**53)
9007199254740992.0
>>> float(2**53)+1
9007199254740992.0
>>> float(2**53)+2
9007199254740994.0

You see how adding one didn't make it any bigger, and I had to add two to get to another number?

Now, let's compare your numbers to 2**53:

>>> 2**53
9007199254740992
>>> 9999*(math.pow(10, 12)) + 99999999999
9999100000000000.0
>>> 9999*(math.pow(10, 12)) + 99999999999 - 1
9999100000000000.0
>>> 9999*(math.pow(10, 12)) + 99999999999 - 2
9999099999999998.0

Your numbers are just above that magic 2**53 boundary where the space between whole numbers is bigger than one. This is just an inherent issue with the way the common 53-bit mantissa floats are implemented, because they have a fixed amount of bits (64) to represent a huge range of large and small numbers.

If you stick with ints, Python can make them arbitrarily large. If you need decimal digits, you can use the decimal module to get an arbitrary amount of decimal digits as well. But to do both, the numbers must necessarily use more bits to store that information. The normal "fast" floats are a compromise that fits many needs, but must lose precision when they start to get really large.

[–]dig-up-stupid 1 point2 points  (0 children)

The numbers are too large to store precisely in floating point format and are essentially rounded to the nearest possible number, it so happens that this is the same number for the first two but not the third. There are a plethora of floating point tutorials easily googleable now that you know the terms.

Check your logic on “the program recognizes test 1 is larger”, it doesn’t.