#Extracting fractional part out of float
33 messages · Page 1 of 1 (latest)
Floats will always have that issue
It's due to the way that floating point values are stored in binary on the computer
No not really, but you most likely will never need it to be that precise
I would suggest using the fraction library
!exec ```py
import fractions
print(0.1 + 0.2)
print(fractions.Fraction(0.1 + 0.2).limit_denominator())
@upper temple
Line 4
print(fractions.Fraction(0.1 + 0.2).limit_denominator()
SyntaxError: '(' was never closed
!exec
@upper temple
0.30000000000000004
3/10
This first example, the 4 only occurs on the 17th decimal place. You will probably not have a use case where that isn't precise enough
Especially if you're doing arithmetic operations
!eval str(0.1+0.2)
>>> str(0.1+0.2)
'0.30000000000000004'
The eval command is currently only available to the server staff.
!eval str(0.1).split(".")[1]
>>> str(0.1).split(".")[1]
'1'
!eval str(0.1+0.2).split(".")[1]
>>> str(0.1+0.2).split(".")[1]
'30000000000000004'
!eval repr(0.1)
>>> repr(0.1)
'0.1'
Idk, you're too hung up on a fantasy ideal of precision when you're limited by the hardware that python runs on
Okay okay
I find that hard to believe
It's a limitation with storing decimal values in binary
So it doesn't matter the language
Thanks 😘
aww