d8aninja
d8aninja

Reputation: 3653

Error handling np.arccos() and classifying triangles in Python

The code below is my attempt at a codewars challenge asking you to calculate the interior angles of a triangle. Really inelegant, brute force. I'm passing all test cases, but also receiving an error:

-c:37: RuntimeWarning: invalid value encountered in arccos
-c:38: RuntimeWarning: invalid value encountered in arccos
-c:39: RuntimeWarning: invalid value encountered in arccos

So I am trying to figure out how my current checks aren't catching invalid values prior to the gamma calculations (noted with ** in the code, below).

THE PROBLEM:

You should calculate type of triangle with three given sides a, b and c (given in any order).

If all angles are less than 90°, this triangle is acute and function should return 1.

If one angle is strictly 90°, this triangle is right and function should return 2.

If one angle more than 90°, this triangle is obtuse and function should return 3.

If three sides cannot form triangle, or one angle is 180° (which turns triangle into segment) - function should return 0.

Input parameters are sides of given triangle. All input values are non-negative floating point or integer numbers (or both).

import numpy 
def triangle_type(a, b, c):
    # Should return triangle type:
    #  0 : if triangle cannot be made with given sides
    #  1 : acute triangle
    #  2 : right triangle
    #  3 : obtuse triangle

    # make sure all sides are non-zero
    if a and b and c:
        pass
    else:
        return 0

    # triangle inequality test
    if not ((c - b) < a < (c + b)) and ((a - c) < b < (a + c)) and ((b - a) < b < (b + a)):
        return 0
    elif a < b < c or b < a < c:
        # print a,b,c
        pass
    elif b < c < a or c < b < a:
        a, c = c, a
        # print a, b, c
    elif c < a < b or a < c < b:
        b, c = c, b
        # print a, b, c
    else:
        # print a, b, c
        pass

    # calculate the gammas **THIS IS WHERE I NEED HELD**
    gamma_1 = numpy.rad2deg(numpy.arccos((a * a + b * b - c * c) / (2.0 * a * b)))
    gamma_2 = numpy.rad2deg(numpy.arccos((c * c + a * a - b * b) / (2.0 * c * a)))
    gamma_3 = numpy.rad2deg(numpy.arccos((b * b + c * c - a * a) / (2.0 * b * c)))

    #check for a right angle
    if (a * a + b * b == c * c) or (b * b + c * c == a * a) or (c * c + b * b == a * a):
        return 2
    #check acute angles
    elif max(gamma_1, gamma_2, gamma_3) < 90.0:
        return 1
    #check obtuse 
    elif max(gamma_1, gamma_2, gamma_3) > 90.0:
        return 3
    else:
        return 0

I can't check the validity of the gamma values without actually making the call, thus generating the error. How can I get around this problem?

Some test cases are

triangle_type(7,3,2) # Not triangle 0
triangle_type(2,4,6) # Not triangle 0
triangle_type(8,5,7) # Acute 1
triangle_type(3,4,5) # Right 2
triangle_type(7,12,8) # Obtuse 3

But this is by no means exhaustive - there are 121 other tests I can't see.

Upvotes: 0

Views: 1028

Answers (1)

Noidea
Noidea

Reputation: 1435

You need to check the values you pass to numpy.arccos. They must be between -1 and 1. But due to floating point calculations they could end up larger than 1 or smaller than -1.

Upvotes: 1

Related Questions