Featured Image

Decoding Trig Functions: Understanding sin(90)’s Surprising Output

Ever wondered why trigonometric functions in programming languages give unexpected results? Let’s explore why sin(90) isn’t 1!

David Techwell
DataFrontiers
Published in
2 min readDec 4, 2023

--

Why Does sin(90) in Code Give a Different Result?

Imagine you’re writing code and you use a trig function like sin(90). You expect a certain result, but what you get is... different. Let's explore why this happens in programming languages.

Here’s a common scenario: you’re working on a project that requires some trigonometry. You confidently type:

sin(90) = 0.8939966636005579

But wait! Shouldn’t sin(90) be 1? This result can be quite confusing, especially if you're new to programming or trigonometry.

The answer lies in understanding how programming languages handle angles. Most programming languages, including Python, Java, and C++, use radians for trigonometric functions, not degrees. So, when you write sin(90), the function assumes 90 radians, not 90 degrees!

One radian is approximately 57.2958 degrees. So, 90 radians is way beyond the 360 degrees of a circle, explaining the unexpected result. The correct way to use the sin function for 90 degrees is to first convert 90 degrees to radians. Here’s how you do it:

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

Now, use this function to convert 90 degrees to radians before calling the sin function:

sin(toRadians(90))

This approach will give you the expected result of 1 for sin(90) in degrees.

It’s important to remember this radians-versus-degrees detail in programming. This principle isn’t just for the sine function; it applies to all trigonometric functions like cosine and tangent. Being aware of this can save you a lot of confusion in your coding adventures.

If you’re using a language like Python, it offers a handy function math.radians() that does the conversion for you:

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

Using this function, you ensure that your trigonometric calculations are accurate and match your expectations. Happy coding!

References and FAQs

References:

Python Math Module — Official documentation for Python’s math module, including math.radians function.

Java Math Class — Official documentation for Java’s Math class, including Math.toRadians method.

C++ XMConvertToRadians Function — Microsoft documentation for the XMConvertToRadians function in C++.

FAQs

Q: Why does sin(90) not equal 1 in my programming language?

A: This is because most programming languages use radians, not degrees, for trigonometric functions. To get the expected result for degrees, you need to convert the angle to radians first.

Q: How do I convert degrees to radians in my code?

A: You can create a function to convert degrees to radians. Python, Java, and C++ have built-in functions for this conversion: math.radians() in Python, Math.toRadians() in Java, and XMConvertToRadians() in C++.

Q: Can I use degrees directly in trigonometric functions?

A: No, you typically need to convert degrees to radians first, as most programming languages’ trigonometric functions are based on radians.

Originally published on HackingWithCode.com.

--

--

David Techwell
DataFrontiers

Tech Enthusiast, Software Engineer, and Passionate Blogger.