Jan-25-2023, 02:36 PM
I'm working through a simple python tutorial and I don't know why I can't get enough point to finish the quiz.
My quiz is,
"
Comparing float numbers is different than comparing integers. As we learned before float numbers are inaccurate at some points. So we should consider this problem when we want to compare 2 float numbers and check if they are equal.
When you compare integers you use the "==" operator and this may cause an error if you try to compare float numbers due to the internal representation of float numbers. Let's see an example to make it more clear. Here "a" and "b" are mathematically equal [explanation: 10 / 3 = (8+2) / 3 = 8/3 + 2/3]:
a = 10/3
b = 8/3 + 2/3
But the stored value in memory may be something similar to this:
a = 3.3333333333333335
b = 3.333333333333333
Now if we compare these numbers with the "==" operator the result will be false!
How do we check the equality of 2 float numbers to avoid this error?
To check the equality of float numbers "a" and "b" we should check the below expression:
abs(a-b) < SMALL_NUMBER
Here "SMALL_NUMBER" is the acceptable error value that can be different based on the hardware and programming language. In this exercise, we will use 10 to the power of -9 which equals 0.00000001.
Now to solve this exercise we should return True if "a" and "b" are almost equal and return false if they are not. Like the code below:
I should use this code block,
Note, I'm not a student or anything, Just testing this tutorial for fun.
My quiz is,
"
Comparing float numbers is different than comparing integers. As we learned before float numbers are inaccurate at some points. So we should consider this problem when we want to compare 2 float numbers and check if they are equal.
When you compare integers you use the "==" operator and this may cause an error if you try to compare float numbers due to the internal representation of float numbers. Let's see an example to make it more clear. Here "a" and "b" are mathematically equal [explanation: 10 / 3 = (8+2) / 3 = 8/3 + 2/3]:
a = 10/3
b = 8/3 + 2/3
But the stored value in memory may be something similar to this:
a = 3.3333333333333335
b = 3.333333333333333
Now if we compare these numbers with the "==" operator the result will be false!
How do we check the equality of 2 float numbers to avoid this error?
To check the equality of float numbers "a" and "b" we should check the below expression:
abs(a-b) < SMALL_NUMBER
Here "SMALL_NUMBER" is the acceptable error value that can be different based on the hardware and programming language. In this exercise, we will use 10 to the power of -9 which equals 0.00000001.
Now to solve this exercise we should return True if "a" and "b" are almost equal and return false if they are not. Like the code below:
Quote:if abs(a-b) < pow(10,-9):
return True
return False
I should use this code block,
Quote:class Solution:I added the lines,
def level7_float_comparison(self, a, b):
"""
Compare float numbers
:type a: float
:type b: float
:rtype: bool
"""
# Write your code here
return False
Quote:SMALL_NUMBER = pow(10,-9)But only got 2/6 of the required score limit to continue. I'm missing something or if's a bug ?
if abs(a-b) < SMALL_NUMBER:
return True
return False
Note, I'm not a student or anything, Just testing this tutorial for fun.
![[Image: Ny-bitmappsbild.png]](https://i.ibb.co/CV7Kd38/Ny-bitmappsbild.png)