Can anyone tell me why i can't compare the value of a function and a float/number. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone tell me why i can't compare the value of a function and a float/number.

# Everything is fine except 7th line from last...it is showing that type error. def get_y(m, b, x): y = m*x + b return y #Write your calculate_error function here def calculate_error(m,b,point): x_point =point[0] y_point =point[1] return abs(get_y(m,b,x_point) - y_point) datapoints = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)] #Write your calculate_all_errodr function here def calculate_all_error(m,b,points): k=0 for z in points: calculate_error(m,b,z) k=k+calculate_error(m,b,z) return k possible_ms = a=[-10 + 0.1 * x for x in range(int((10-(-10))/0.1)+1)] possible_bs = a=[-10 + 0.1 * x for x in range(int((20-(-20))/0.1)+1)] smallest_error=float("inf") best_m=0 best_b=0 for m in possible_ms: for b in possible_bs: if calculate_all_error(m,b,datapoints) < smallest_error: best_m=m best_b=b smallest_error=calculate_all_error print(best_m) print(best_b) print(smallest_error)

2nd Aug 2020, 11:40 AM
Abhishek Kumar
Abhishek Kumar - avatar
8 Answers
+ 4
I think this causing error : You are assigning function pointer to smallest_error in smallest_error=calculate_all_error It may smallest_error=calculate_all_error(m,b,datapoints)?
2nd Aug 2020, 11:55 AM
Jayakrishna 🇮🇳
+ 5
i found this in Stack Overflow: inf is infinity - a value that is greater than any other value. -inf is therefore smaller than any other value.
2nd Aug 2020, 1:18 PM
Lothar
Lothar - avatar
+ 2
Slick that's not string bro inf is infinity in python and it is written as float("inf") not float(inf)
2nd Aug 2020, 12:29 PM
Abhishek Kumar
Abhishek Kumar - avatar
+ 1
Lothar yeah exactly..this the definition from the very basic of mathematics. that inf is the largest while -inf is smallest number..that's why they are named so.
2nd Aug 2020, 2:08 PM
Abhishek Kumar
Abhishek Kumar - avatar
0
it also looks like you are comparing an integer and a string. your line: smallest_error = float('inf') is equal to a string https://code.sololearn.com/cg7Dv16z7n2I/?ref=app
2nd Aug 2020, 12:18 PM
Slick
Slick - avatar
0
Btw i got where i was committing mistake it...I should have given the arguments to calculate_all_error Function in the 4th line from last.
2nd Aug 2020, 12:30 PM
Abhishek Kumar
Abhishek Kumar - avatar
0
Jayakrishna has been showing me this. Im super wrong there. Its gotta be in one of the functions then
2nd Aug 2020, 12:31 PM
Slick
Slick - avatar
0
Jayakrishna🇮🇳 exactly thank you.
2nd Aug 2020, 1:13 PM
Abhishek Kumar
Abhishek Kumar - avatar