Skip to content

Instantly share code, notes, and snippets.

@chipbuster
Created January 30, 2023 15:59
Show Gist options
  • Save chipbuster/90abef9f662d3a6e0d63fb1655422421 to your computer and use it in GitHub Desktop.
Save chipbuster/90abef9f662d3a6e0d63fb1655422421 to your computer and use it in GitHub Desktop.
A Demonstration of Floating Point Precision in Python
# In principle, this should be simple enough to reconstruct on the fly
# In practice, every time I come back to it, it takes me 10-20 minutes
# to recall the correct sequence of operations to get the dang thing
# to diverge, so I'm going to gist this.
x = 1/3
print(f"Initially, x is {x}")
for _ in range(540):
x *= 4.0
x -= 1.0
print(f"After executing x = 4x - 1, x is {x}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment