Created
February 9, 2022 07:42
-
-
Save wonderbeyond/0ab0410b4bda7f2e737d40f5070432c5 to your computer and use it in GitHub Desktop.
Demo script to show float type could miss precision
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
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