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
  rounding and floats Than999 2 593 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  put an image into 3 parts Nickd12 4 814 Sep-30-2020, 11:00 PM
Last Post: Nickd12
  int, floats, eval menator01 2 787 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 899 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  Alias for different parts of a variable mln4python 3 962 Aug-28-2019, 03:03 PM
Last Post: snippsat
  extract from strings some parts dervast 1 837 Jul-04-2019, 09:44 PM
Last Post: Axel_Erfurt
  Integer vs Floats Tazbo 2 1,405 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  Formatting floats Irhcsa 6 1,698 Oct-04-2018, 04:23 PM
Last Post: volcano63
  How to merge 2 parts of script br0ken 0 1,320 Dec-18-2017, 07:32 PM
Last Post: br0ken
  How to use a list of floats kristrek 14 5,842 Jan-26-2017, 05:33 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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