Python Forum
comparing fractional parts of floats
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing fractional parts of floats
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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)
Reply
#3
> 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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Is round(x, 1) not working?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 650 Nov-12-2023, 11:53 AM
Last Post: PyDan
  floats 2 decimals rwahdan 3 1,588 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,046 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,403 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 2,956 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding floats to a number of bits Skaperen 2 2,271 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  Integer vs Floats Tazbo 2 2,843 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  Formatting floats Irhcsa 6 4,067 Oct-04-2018, 04:23 PM
Last Post: volcano63
  How do you sort a table by one column?? (of floats) sortedfunctionfails 3 12,197 Jan-11-2018, 09:04 AM
Last Post: sortedfunctionfails
  Reading floats and ints from csv-like file Krookroo 15 19,901 Sep-05-2017, 03:58 PM
Last Post: Krookroo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020