64

I'm working on a problem that has to do with calculating angles of refraction and what not. However, it seems that I'm unable to use the numpy.sin() function in degrees. I have tried to use numpy.degrees() and numpy.rad2deg().

numpy.sin(90)

numpy.degrees(numpy.sin(90))

Both return ~ 0.894 and ~ 51.2 respectively.

Thanks for your help.

Daniil Ukhorskiy
  • 653
  • 1
  • 5
  • 5

6 Answers6

93

You don't want to convert to degrees, because you already have your number (90) in degrees. You need to convert 90 from degrees to radians, and you need to do it before you take the sine:

>>> np.sin(np.deg2rad(90))
1.0

(You can use either deg2rad or radians.)

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
20

Use the math module from the standard Python library:

>>> math.sin(math.radians(90))
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • 7
    One problem would be that it isn't vectorized while the numpy solution above is. But, that comes down to if you are using numpy or not... – Jon Custer Jan 21 '15 at 23:44
  • 1
    What do you mean vectorized? They both return the same number. – Malik Brahimi Jan 21 '15 at 23:47
  • 7
    But the numpy functions will take a numpy array and return an array. If you do lots of math, you should check out numpy... – Jon Custer Jan 22 '15 at 00:31
  • 2
    Yeah, NumPy is great, but sometimes it is better to avoid dependencies and go with the standard library. ^^ – Peque Jun 15 '17 at 19:15
3

You can define the following symbols to work in degrees:

sind = lambda degrees: np.sin(np.deg2rad(degrees))
cosd = lambda degrees: np.cos(np.deg2rad(degrees))
print(sind(90)) # Output 1.0
Freeman
  • 5,810
  • 3
  • 47
  • 48
2

As a warning, none of these answers will work for very large inputs. numpy's deg2rad function simply multiplies the argument by pi/180. The source for this is here (at the C level).

Imprecision in this value will result in horrible error. For example:

import numpy

def cosd(x):
    return numpy.cos(numpy.deg2rad(x))

print(cosd(1.0E50)) # Prints -0.9999338286702031

Let's try this in C with some standard library tricks.

#include <stdio.h>
#include <math.h>

#define cosd(x) cos(fmod((x), 360.0) * M_PI / 180.0)

int main(void)
{
    const double x = 1.0E50;
    printf("%f\n", cosd(x));
    return 0;
}

This prints out 0.766044, so our cosd function in Python is off by about 2, when the function is bounded between -1 and 1!

It seems numpy has a mod function. Let's duplicate this C routine using that.

import numpy

def cosd(x):
    return numpy.cos(numpy.deg2rad(numpy.mod(x, 360.0)))

print(cosd(1.0E50)) # 0.7660444431189778

And all is well.

0

Multiplying by pi/180 perform the conversion from degrees to radians. So, np.sin(90*np.pi/180) works as well.

romain gal
  • 348
  • 2
  • 9
0

FIY. SciPy provides the sin function which accepts degrees: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.sindg.html#scipy.special.sindg

In [2]: import scipy.special as special

In [3]: special.sindg(90)
Out[3]: 1.0
Atsushi Sakai
  • 938
  • 10
  • 22