Python Forum
comparing fractional parts of floats - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: comparing fractional parts of floats (/thread-16877.html)



comparing fractional parts of floats - Skaperen - Mar-18-2019

a function i am writing will get a value that originates as a whole value and ONE digit past the decimal point. in this function, which may get the value as float, i need to determine which digit was the original value. direct comparison is unsafe due to the inexact representations most of these values will have (X.0 and X.5 are the values that can be represented exactly). i am looking for a way to convert the fractional value ranging from X.0 to X.9 into 0 to 9 (what the next part of the code will be working with). so i will not be doing comparisons like:
    f,w = modf(number)
    if f == 0.0:
        n = 0
    if f == 0.1:
        n = 1
    if f == 0.2:
        n = 2
    if f == 0.3:
        n = 3
    if f == 0.4:
        n = 4
    if f == 0.5:
        n = 5
    if f == 0.6:
        n = 6
    if f == 0.7:
        n = 7
    if f == 0.8:
        n = 8
    if f == 0.9:
        n = 9
what would be the best way to do this, to get the original digit_after_the_point when it comes in the form of float.


RE: comparing fractional parts of floats - nilamo - Mar-18-2019

Maybe multiply by 10, and just use the integer part of the float?
>>> x = 1.32 / 0.942
>>> x
1.4012738853503186
>>> import math
>>> left = math.floor(x)
>>> left
1
>>> x -= left
>>> x
0.4012738853503186
>>> right = math.floor(x * 10)
>>> left, right
(1, 4)



RE: comparing fractional parts of floats - Skaperen - Mar-18-2019

> Maybe multiply by 10, and just use the integer part of the float?

i've tried that. it failed in a case where an imprecise value for 0.7 was 0.6999 and gave me 6 when i should have had 7. i have found that str() rounds it up when it gets the float like that. so maybe i can do int(str(value)[0]). that or add 0.5 to the value after multiplying it by 10, and then pass that to int(). apparently, int() just truncates rather than rounds. i saw that when i tried to use it to do what math.modf() does.


RE: comparing fractional parts of floats - ichabod801 - Mar-18-2019

Is round(x, 1) not working?


RE: comparing fractional parts of floats - casevh - Mar-19-2019

Starting with an example that fails the "multiply by 10" approach:

>>> math.modf(22.7)[0]*10
6.999999999999993
You can use the fractions module:

Note: the following fails when the denominator is either 1, 2 or 5.

>>> from fractions import Fraction
>>> Fraction(22.7)
Fraction(6389481971331891, 281474976710656)
>>> Fraction(22.7).limit_denominator(10)
Fraction(227, 10)
>>> a=Fraction(22.7).limit_denominator(10)
>>> divmod(a.numerator, a.denominator)
(22, 7)
The following version should handle the cases when the denominator is not 10. It also uses the output of modf directly.

>>> a=Fraction(math.modf(22.7)[0]).limit_denominator(10)
>>> divmod(10,a.denominator)[0]*a.numerator
7