Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 comparing fractional parts of floats Skaperen Black Knight Posts: 3,238 Threads: 1,026 Joined: Sep 2016 Reputation: 13 Likes received: 101 #1 Mar-18-2019, 07:31 PM 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. What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American. nilamo Last Thursdayist Posts: 3,257 Threads: 87 Joined: Sep 2016 Reputation: 133 Likes received: 731 #2 Mar-18-2019, 07:45 PM 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)``` Skaperen Black Knight Posts: 3,238 Threads: 1,026 Joined: Sep 2016 Reputation: 13 Likes received: 101 #3 Mar-18-2019, 10:24 PM > 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. What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American. ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #4 Mar-18-2019, 10:29 PM Is `round(x, 1)` not working? Skaperen likes this post Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures casevh Silly Frenchman Posts: 35 Threads: 1 Joined: Oct 2016 Reputation: 8 Likes received: 22 #5 Mar-19-2019, 03:19 AM (This post was last modified: Mar-19-2019, 03:44 AM by casevh. Edited 1 time in total. Edit Reason: Fix error. ) 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 ``` Skaperen likes this post « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Integer vs Floats Tazbo 2 658 Jan-09-2019, 12:06 PM Last Post: Gribouillis Formatting floats Irhcsa 6 793 Oct-04-2018, 04:23 PM Last Post: volcano63 How to use a list of floats kristrek 14 3,875 Jan-26-2017, 05:33 AM Last Post: snippsat Negative numbers and fractional powers Flexico 1 1,894 Dec-08-2016, 04:12 PM Last Post: Larz60+ Fractional Exponent Question Flexico 2 2,145 Dec-04-2016, 01:00 AM Last Post: Flexico

Forum Jump:

Users browsing this thread: 1 Guest(s)