language-agnostictrigonometrydegreesradians

Why do trigonometric functions give a seemingly incorrect result?


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)


Solution

  • 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.

    >>> import math
    >>> math.sin(math.radians(90))
    1.0
    

    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 :)

    PI = 3.1415926535897932; // or your programming language's standard library's pi constant
    
    function toRadians(degrees) {
      return degrees * (PI / 180);
    }
    

    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

    (trig function docs | conversion function docs, see also How can I convert radians to degrees with Python?)

    >>> import math
    >>> math.sin(math.radians(90))
    1.0
    

    (trig function docs* | conversion function docs)

    System.out.println(Math.sin(Math.toRadians(90.0)));
    

    (trig function docs | conversion function docs)

    Use the functions suffixed with d, which take degrees as an argument directly. For example

    sind(90)
    

    You can also use the deg2rad function if you need to convert degrees to radians.

    Languages that don't include a standard library function

    (trig function docs)

    #include <stdio.h>
    #include <math.h>
    
    double deg2rad(double degrees) {
        return (M_PI / 180) * degrees;
    }
    
    int main() {
        printf("%f", sin(deg2rad(90.0)));
    }
    

    (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.)

    (trig function docs)

    #include <cmath>
    #include <iostream>
    #include <numbers>
    
    double deg2rad(double degrees)
    {
        // `std::numbers::pi` is `double`, for `float` use `std::numbers::pi_v<float>`.
        return (std::numbers::pi / 180) * degrees;
    }
    
    int main()
    {
        std::cout << std::sin(deg2rad(90.0)) << '\n';
    }
    

    (trig function docs*, see also the Remarks sections on the docs for Sin, Cos, Tan)

    using Math;
    
    public static double DegreesToRadians(double degrees)
    {
        return (Math.PI / 180) * degrees;
    }
    
    public static int Main() {
        Console.WriteLine(Math.Sin(DegreesToRadians(90.0)).ToString());
        return 0;
    }
    

    (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?)

    function degToRad(degrees) {
      return degrees * (Math.PI / 180);
    }
    
    console.log(Math.sin(degToRad(90)));
    

    * This programming language doesn't group their trig functions together in their documentation. Look for functions named sin, cos, tan, or similar.