BugLab010 / 020up to 100
floatsprecisioncomparisongotchamedium

Almost Equal

The function is_right_triangle(a, b, c) returns True if a triangle with sides a, b, c is a right triangle. The sides can be given in any order.

It works perfectly for integer sides like 3, 4, 5. But with decimal values like 0.5, 1.2, 1.3 — a perfectly valid right triangle — it returns False.

The formula is correct. The math is correct. So why does it fail?

hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def is_right_triangle(a, b, c):
"""
Returns True if a triangle with sides a, b, c is a right triangle.
The sides can be given in any order.
Example: is_right_triangle(3, 4, 5) -> True
"""
sides = sorted([a, b, c])
return sides[0] ** 2 + sides[1] ** 2 == sides[2] ** 2
>>> is_right_triangle(0.5, 1.2, 1.3)