Skip to content

Instantly share code, notes, and snippets.

@wonderbeyond
Created February 9, 2022 07:42
Show Gist options
  • Save wonderbeyond/0ab0410b4bda7f2e737d40f5070432c5 to your computer and use it in GitHub Desktop.
Save wonderbeyond/0ab0410b4bda7f2e737d40f5070432c5 to your computer and use it in GitHub Desktop.
Demo script to show float type could miss precision
import random
for _ in range(100):
f1 = round(random.random(), 2) + random.randint(1, 1e8)
f2 = round(random.random(), 2) + random.randint(1, 1e8)
r = f2 - f1
r_round2 = round(r, 2)
if not r == r_round2:
print(f'{f2} - {f1} = {r}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment