Reputation: 13
So I am trying to create a function that compares how closely two float values are the same, then the program should return the approximate value for the parts where the values are the same.
For example, when the function is given the numbers:
3.141593353533333
and 3.142222222222222
the function should return the value: 3.142
3.14222
and 3.14159
the function should return the value: 3.142
4.19999
and 4.19977
the function should return the value 4.2
I've been approaching this trying to round the numbers to match. The function I made seems to work but does not always return the correct values due to rounding problems.
The function returns 4.2
for the 3rd values but 3.14
for the 1st and 2nd values.
This is because there are 2 common decimals for the 1st and 2nd values and when the rounding by 2 decimals you get 3.14
instead of 3.142
How do I get around this problem? I have a feeling I have to re-think my approach completely but I am unsure as to how and naturally adding +1 to the rounding point manually isn't an option.
Here is what I've managed to make so far:
def lukuvertailu(a, b):
a_str = str(a).split(".") #split the given float value at the point
b_str = str(b).split(".")
min_len = min(len(a_str[1]), len(b_str[1])) #get the mininum amount of decimals from either value
i = 0 #counter for the amount of matching decimals
if (a_str[0] == b_str[0]): #check if the whole number matches.
while i < min_len and a_str[1][i] == b_str[1][i]: #check if an decimal matches for both values.
i += 1 #if it does, add 1 to the counter
else:
return "-" #if the whole number does not match, just return "-"
num1 = round(a, i)
num2 = round(b, i) #round the values based on the number of matching decimals
if (num1 == num2):
return (num1) #now the approximate value for the parts where the values are the same should be found.
Upvotes: 0
Views: 99
Reputation: 308520
You can keep the first part of your answer for comparing the whole parts of the numbers, although it might not do what you want for comparing e.g. 123.9 and 124.1.
To compare the fractional part merely requires a simple loop:
i = 1
while round(a, i) == round(b, i) and i < 17:
i += 1
return round(a, i-1)
Upvotes: 0
Reputation: 347
Here is a simple python function to find highest approximate rounded number between two numbers:
def find_approx_round(a, b):
a_list = []
b_list = []
for i in range(len(str(a).split('.')[1])):
a_list.append(round(a, i))
for i in range(len(str(b).split('.')[1])):
b_list.append(round(b, i))
try:
return max(set(a_list).intersection(b_list))
except ValueError:
print('Rounded Arithmetic Mean')
return round((a+b)/2, 10)
It is primarily intended to work with numbers like you mentioned that are close to each other, otherwise if they are far apart, it will just display the rounded arithmetic mean.
Upvotes: 0