Last active
November 23, 2016 20:50
-
-
Save mverleg/8adb220c64d113fe8b8c093f1d69572a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python 3 + numpy | |
from numpy import iinfo, int64 | |
# iinfo(int64).max will give the maximum value of an 8-byte precision integer, | |
# which should be 2^63 - 1 = 9223372036854775807 | |
# (there is one bit for the sign, and a shift by 1 because of zero) | |
# The trick is that a 8-byte float uses 11 bits for the exponent | |
# which leaves 63 - 11 = 52 for the significand, | |
# but it's first digit is implicitly a 1, so 53 | |
# Thus, dividing by 2**10 brings it to the largest integer such that | |
# it and any smaller integer can be represented exactly as a float | |
# Actually, that number minus 1, due to the -1 above and rounding down. | |
q = iinfo(int64).max // 2**10 | |
# This just sents a limit that's a bit higher than q, | |
# which won't be reached because of precision loss (see later) | |
q_lim = q + 3 | |
# This loop will print "hello" several times | |
# and then terminate if q increases enough, but it won't. | |
while q < q_lim: | |
print('hello') | |
# Because we used `1.`, q will be converted to a double precision float. | |
# But it will have the highest possible exact value. | |
# Increasing the value by 1 will result in it not being exactly represented | |
# and as such rounded (down). Thus, q will stay the same. | |
q += 1. | |
# So this program will run forever (or more likely, until you terminate it). | |
# As a bonus, note that `iinfo(int64).max // 2**10` is NOT the highest integer | |
# exactly represented as a float64, it's just the highest where all lower | |
# integers can also be represented. | |
# In fact, every higher number that can be represented as a float64 is an integer, | |
# because there isn't enough precision for decimals. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment