Range of trigonometric functions in python


from math import * 

def f(a,b,c,d): 
    alpha = arccos(a/d)
    beta = pi - arcsin(a+b/d) 
    theta= arccos(a/c)
    eta= arccos (a+c/b+d) 
    return (alpha, beta, theta, eta)

I was thinking during this that there there is a possibility that the terms inside arccos and arcsin would be out of range. so I should check in every function if the terms are within the range or not i.e. by use if statement example: if a/d>1 or a/d<1: alpha=arccos(a/d) but if I want to make this for every line, this will make the code looks ugly, plus what in case we have many trigonometric functions. Is there a function or a method to do that for all cosine and sines used in python?

(Note that this function above is written randomly)


Firstly, please don’t use examples that are "written randomly". An example of why is that the functions in math are called acos and asin, not arccos and arcsin and by actually running the code you would spot this.

math.acos raises ValueError with the message "math domain error" when given an argument > 1. You can catch all instances of this like so.

from math import acos, asin

def f(a,b,c,d):
        alpha = acos(a/d)
        beta = pi - asin(a+b/d) 
        theta = acos(a/c)
        eta = acos (a+c/b+d) 
        return alpha, beta, theta, eta
    except ValueError:
        print("Got an argument outside [-1, 1]")

Answered By – MichaelCG8

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.