Error handling np.arccos() and classifying triangles in Python
<p>The code below is my attempt at a codewars challenge asking you to <a href="https://www.codewars.com/kata/53907ac3cd51b69f790006c5/train/python" rel="nofollow">calculate the interior angles of a triangle</a>. Really inelegant, brute force.  I'm passing all test cases, but also receiving an error: </p>

<pre><code>-c:37: RuntimeWarning: invalid value encountered in arccos
-c:38: RuntimeWarning: invalid value encountered in arccos
-c:39: RuntimeWarning: invalid value encountered in arccos
</code></pre>

<p>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). </p>

<p>THE PROBLEM:</p>

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

<p>If all angles are less than 90Â°, this triangle is acute and function should return 1.</p>

<p>If one angle is strictly 90Â°, this triangle is right and function should return 2.</p>

<p>If one angle more than 90Â°, this triangle is obtuse and function should return 3.</p>

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

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

<pre><code>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) &lt; a &lt; (c + b)) and ((a - c) &lt; b &lt; (a + c)) and ((b - a) &lt; b &lt; (b + a)):
        return 0
    elif a &lt; b &lt; c or b &lt; a &lt; c:
        # print a,b,c
        pass
    elif b &lt; c &lt; a or c &lt; b &lt; a:
        a, c = c, a
        # print a, b, c
    elif c &lt; a &lt; b or a &lt; c &lt; 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) &lt; 90.0:
        return 1
    #check obtuse 
    elif max(gamma_1, gamma_2, gamma_3) &gt; 90.0:
        return 3
    else:
        return 0
</code></pre>

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

<p>Some test cases are</p>

<pre><code>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
</code></pre>

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

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

python numpy math triangulation