Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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):
    try:
        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]")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading