When attempting to use a trigonometric function in my programming language, I get a seemingly wildly incorrect result.
For example,
sin(90) = 0.8939966636005579
But I would expect sin(90)
to be 1
. Similar things happen for other trigonometric functions.
What is going on and what can I do to fix this?
This is intended to be a canonical question for input unit requirements in trigonometric functions. (related discussion on meta)
Why is this happening?
Trigonometric functions generally expect units in radians, while
90
is in degrees.This also applies for other functions such as cosine and tangent, not just the sine function.
Most programming languages require trigonometric functions to provide their arguments in radians. This simplifies many things, especially on the implementation side as code may interface with C, which is standardized to expect that its arguments to the trig functions in radians.
It also happens to be that the radians are used very commonly in math as they are a natural unit for angle measurement. This Math.SE question and the answers there go into more detail.
Some programming languages may provide facilities to make the conversion in its standard library. For example, in Python this is provided by the
math.radians
function. The trigonometric functions themselves generally also say what format they expect their inputs to be in.This is the case in some programming language's standard libraries, such as Rust, Java, Python, MATLAB, and others.
However, many programming languages don't provide facilities to convert degrees to radians or back, but an implementation is easy. You can do so by creating a function like so :)
After you've converted from degrees to radians, you can then call the trigonometric functions and get the answers you're expecting.
What are some examples that I can use?
It is helpful to consult your programming language's standard library and documentation to see if a function for conversion exists in your programming language before writing your own.
Languages that include a standard library function
python (trig function docs | conversion function docs, see also How can I convert radians to degrees with Python?)
java (trig function docs* | conversion function docs)
matlab (trig function docs | conversion function docs)
Use the functions suffixed with
d
, which take degrees as an argument directly. For exampleYou can also use the
deg2rad
function if you need to convert degrees to radians.Languages that don't include a standard library function
c (trig function docs)
(Note: Not all implementations define
M_PI
, although many do, including glibc which says it comes from Unix98 and 4.4BSD. If yours doesn't, you'll need to define it.)c++ (trig function docs)
c# (trig function docs*, see also the Remarks sections on the docs for
Sin
,Cos
,Tan
)javascript (trig function docs on MDN*, specifically the Converting between degrees and radians section, see also How can I get sin, cos, and tan to use degrees instead of radians?)
* This programming language doesn't group their trig functions together in their documentation. Look for functions named
sin
,cos
,tan
, or similar.