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


Messages In This Thread
comparing fractional parts of floats - by Skaperen - Mar-18-2019, 07:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 651 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,404 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,272 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,070 Oct-04-2018, 04:23 PM
Last Post: volcano63
  How do you sort a table by one column?? (of floats) sortedfunctionfails 3 12,199 Jan-11-2018, 09:04 AM
Last Post: sortedfunctionfails
  Reading floats and ints from csv-like file Krookroo 15 19,905 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