Skip to content

Instantly share code, notes, and snippets.

@hyamamoto
Last active September 19, 2016 10:27
Show Gist options
  • Save hyamamoto/52ce752cde77eae63b81 to your computer and use it in GitHub Desktop.
Save hyamamoto/52ce752cde77eae63b81 to your computer and use it in GitHub Desktop.
Calculating float values in Python with builtins.
#!/usr/bin/env python
def p(t, v):
print('{:<40}-> {}'.format(t, v))
p('.1 + .1 + .1', .1 + .1 + .1)
p('round(.1 + .1 + .1, 10)', round(.1 + .1 + .1, 10))
p('(0.1).as_integer_ratio()', (0.1).as_integer_ratio())
import math
p('sum([0.1] * 10) == 1.0', sum([0.1] * 10) == 1.0)
p('math.fsum([0.1] * 10) == 1.0', math.fsum([0.1] * 10) == 1.0)
from fractions import Fraction
from decimal import Decimal
p('format(0.1, \'.17f\')', format(0.1, '.17f'))
p('format(Decimal.from_float(0.1), \'.17\')', format(Decimal.from_float(0.1), '.17'))
p('Fraction.from_float(0.1)', Fraction.from_float(0.1))
p('Decimal.from_float(0.1)', Decimal.from_float(0.1))
.1 + .1 + .1 -> 0.3
round(.1 + .1 + .1, 10) -> 0.3
(0.1).as_integer_ratio() -> (3602879701896397, 36028797018963968)
sum([0.1] * 10) == 1.0 -> False
math.fsum([0.1] * 10) == 1.0 -> True
format(0.1, '.17f') -> 0.10000000000000001
format(Decimal.from_float(0.1), '.17') -> 0.10000000000000001
Fraction.from_float(0.1) -> 3602879701896397/36028797018963968
Decimal.from_float(0.1) -> 0.1000000000000000055511151231257827021181583404541015625
.1 + .1 + .1 -> 0.30000000000000004
round(.1 + .1 + .1, 10) -> 0.3
(0.1).as_integer_ratio() -> (3602879701896397, 36028797018963968)
sum([0.1] * 10) == 1.0 -> False
math.fsum([0.1] * 10) == 1.0 -> True
format(0.1, '.17f') -> 0.10000000000000001
format(Decimal.from_float(0.1), '.17') -> 0.10000000000000001
Fraction.from_float(0.1) -> 3602879701896397/36028797018963968
Decimal.from_float(0.1) -> 0.1000000000000000055511151231257827021181583404541015625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment