Python Forum
I'm trying to figure out if this is a bug from a simple quiz at Future Skill
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm trying to figure out if this is a bug from a simple quiz at Future Skill
#1
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:

Quote:if abs(a-b) < pow(10,-9):
return True
return False

I should use this code block,

Quote:class Solution:
def level7_float_comparison(self, a, b):
"""
Compare float numbers

:type a: float
:type b: float

:rtype: bool
"""
# Write your code here
return False
I added the lines,

Quote:SMALL_NUMBER = pow(10,-9)
if abs(a-b) < SMALL_NUMBER:
return True
return False
But only got 2/6 of the required score limit to continue. I'm missing something or if's a bug ?
Note, I'm not a student or anything, Just testing this tutorial for fun.

[Image: Ny-bitmappsbild.png]
Reply
#2
You can use numbers like 1e-9 instead of using the pow() function.
def equal(a, b, zero=1e-9):
    return abs(a-b) < zero

print(equal(1.1, 1.1))
print(equal(1.1, 1.1 + 1e-8))
print(equal(1.1, 1.1 + 1e-9))   # sketchy
print(equal(1.1, 1.1 + 1e-10))
Output:
True False False True
This works fairly well, but what if the numbers you are testing are near zero? If I compare 1e-9 and 1e-9+1e-10 the test will say they are equal even though they differ by 10%. I don't think using a fixed threshold is the correct way to solve this problem.
Reply
#3
(Jan-25-2023, 03:21 PM)deanhystad Wrote: You can use numbers like 1e-9 instead of using the pow() function.
def equal(a, b, zero=1e-9):
    return abs(a-b) < zero

print(equal(1.1, 1.1))
print(equal(1.1, 1.1 + 1e-8))
print(equal(1.1, 1.1 + 1e-9))   # sketchy
print(equal(1.1, 1.1 + 1e-10))
Output:
True False False True
This works fairly well, but what if the numbers you are testing are near zero? If I compare 1e-9 and 1e-9+1e-10 the test will say they are equal even though they differ by 10%. I don't think using a fixed threshold is the correct way to solve this problem.

I'm trying to write my code from the function def level7_float_comparison(self, a, b):
But I'm baffled why I'm only getting 2 points and not 6/6 points.
Reply
#4
My guess would be that there are 4 tests that you fail. Do you know all 6 tests?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I can't figure out simple rounding to 2 decimal places! Funnypants 7 3,288 Nov-01-2018, 01:38 AM
Last Post: j.crater
  Need To Create Simple Mutiple Choice Quiz In Tkinter Rashi 5 15,462 Apr-01-2017, 04:03 AM
Last Post: Rashi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020