Inverse Cosine in Python

Viewed 121190

Apologies if this is straight forward, but I have not found any help in the python manual or google.

I am trying to find the inverse cosine for a value using python.

i.e. cos⁻¹(x)

Does anyone know how to do this?

Thanks

7 Answers

The result of math.acos() is in radians. So you need to convert that to degrees.

Here is how you can do:

import math
res = math.degrees (math.acos (value))

You may also use arccos from the module numpy

>>> import numpy
>>> numpy.arccos(0.5)
1.0471975511965979

WARNING: For scalars the numpy.arccos() function is much slower (~ 10x) than math.acos. See post here

Nevertheless, the numpy.arccos() is suitable for sequences, while math.acos is not. :)

>>> numpy.arccos([0.5, 0.3])
array([ 1.04719755,  1.26610367])

but

>>> math.acos([0.5, 0.3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
Related