Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get ValueError : math domain error?

Tags:

python

I wrote a function named analyze_the_shape that takes a list of 2D vertices such that the list is in the order of a clockwise traversal of the vertices in the 2D Euclidean space.

I call it in the interpreter and give [(0, 0), (0, 4.0), (4.0, 4.0), (4.0, 0)] as input but I get ValueError : math domain error. I expect to see return ["SQUARE", 4.0]. What can I do ?

import math

def analyze_the_shape(liste):
    if len(liste) == 2 :
        d = ( (liste[1][0] - liste[0][0])**2 + (liste[1][1] - liste[0][1])**2 )**(0.5)   
        return ["LINESEGMENT", d ] 
    if len(liste) == 4 :
        d1 = abs(( (liste[1][0] - liste[0][0])**2 + (liste[1][1] - liste[0][1])**2 )**(0.5))
        d2 = abs(( (liste[2][0] - liste[1][0])**2 + (liste[2][1] - liste[1][1])**2 )**(0.5))
        d3 = abs(( (liste[3][0] - liste[2][0])**2 + (liste[3][1] - liste[2][1])**2 )**(0.5))
        d4 = abs(( (liste[0][0] - liste[3][0])**2 + (liste[0][1] - liste[3][1])**2 )**(0.5)) 
        hypo = abs(( (liste[2][1] - liste[0][1])**2 + (liste[2][0] - liste[0][0])**2 )**(0.5))
        cos_angle = float((hypo**2 - (d3)**2 + (d4)**2) / ((-2.0)*(d4)*(d3)))
        angle = math.degrees(math.acos(cos_angle))
        if d1 == d2 == d3 == d4 and abs(angle - 90.0) < 0.001 :
            return ["SQUARE", d1]  

This is the error I get:

>>> import a
>>> a.analyze_the_shape([(0, 0), (0, 4.0), (4.0, 4.0), (4.0, 0)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "a.py", line 15, in analyze_the_shape

ValueError: math domain error
like image 433
Ali Candan Avatar asked Nov 30 '12 00:11

Ali Candan


People also ask

How do I fix domain error ValueError in math?

The domain of a function is the set of all possible input values. If Python throws the ValueError: math domain error, you've passed an undefined input into the math function. In our case, don't calculate the log of a negative number or zero, and it will resolve the error.

What does math domain error mean?

A domain error occurs if an input argument is outside the domain over which the mathematical function is defined. Paragraph 3 states. A pole error (also known as a singularity or infinitary) occurs if the mathematical function has an exact infinite result as the finite input argument(s) are approached in the limit.

What does domain error mean in Python?

Math domain error in Python means that the input values to a function are not in the function's domain. For example, the domain of the function f(x) = x2 is the set of all real numbers, so if we try to input a complex number into the function, we will get a math domain error.

How do you avoid domain error in Python?

To solve the math domain error in Python, we need to prevent the user so that he cannot calculate the square root of a negative number before we execute the math. sqrt() function.


1 Answers

This exception means that cos_angle is not a valid parameter for math.acos.

Specifically, in this example, it is just below -1, which is out of acos definition.

You could probably try to force your returned cos_angle within [-1,1] with something like :

def clean_cos(cos_angle):
    return min(1,max(cos_angle,-1))

However, this will not return SQUARE, since cos_angle is more or less equals to -1 in your example, and angle thus equals 180. There is probably an issue with your computation before the exception.

like image 130
Julien Vivenot Avatar answered Sep 23 '22 21:09

Julien Vivenot