Why do trigonometric functions give a seemingly incorrect result?

Why do trigonometric functions give a seemingly incorrect result?


12

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.

What is going on and what can I do to fix this?

7

  • Does this answer your question? Math.Sin() gives incorrect value

    – trincot

    yesterday

  • 6

    @trincot this is intended as a canonical QA pair which is language agnostic. See the question on meta

    – Justine Krejcha

    yesterday


  • 1

    I saw the question on meta; I disagree that a new Q&A is needed.

    – trincot

    yesterday

  • 1

    It's a question that is language agnostic as it applies to almost all programming languages. The answers, across all programming languages, come down to about 2 (use the library function to make the conversion or write it yourself).

    – Justine Krejcha

    yesterday


  • 2

    @trincot "I take note that you are saying this topic is not language agnostic" – I genuinely can't fathom the reasoning here. The fact that someone wrote a language-specific QA pair does not refute that the topic is language-agnostic. Nor does the fact of minor syntactic details, differences in library symbol names etc. (enough that code shown for S, whatever that is, would be invalid in Java) make the topic language-specific – as users of any programming language can be taught what a radian is and what pi is.

    – Karl Knechtel

    17 hours ago

1 Answer
1


17

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.

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.1415926535; // or your programming language's standard library's pi constant

function toRadians(degrees) {
  return degrees * (PI / 180);
}

Some programming languages however may provide facilities to make the conversion in its standard library (and this should be preferred over writing a conversion yourself). 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.

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)

sin(deg2rad(90))

Languages that don’t include a standard library function

(trig function docs)

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

float deg2rad(float degrees) {
    return (M_PI / 180) * degrees;
}

int main() {
    printf("%f", sin(deg2rad(90.0)));
}

(trig function docs)

#include <cmath>
#include <iostream>
#include <numbers>

float deg2rad(float degrees)
{
    return (std::numbers::pi_v<float> / 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.

1

  • 1

    For C at least, since the standard sin function uses double, not float, so should your deg2rad function. You could also introduce deg2radf (using float) and deg2radl(using long double) by analogy with the trig functions.

    – jcaron

    40 mins ago



Leave a Reply

Your email address will not be published. Required fields are marked *