Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

In this short tutorial, we'll look at how to calculate sine values using Java's Math.sin() function and how to convert angle values between degrees and radians.

2. Radians vs. Degrees

By default, the Java Math library expects values to its trigonometric functions to be in radians.

As a reminder, radians are just another way to express the measure of an angle, and the conversion is:

double inRadians = inDegrees * PI / 180;
inDegrees = inRadians * 180 / PI;

Java makes this easy with toRadians and toDegrees:

double inRadians = Math.toRadians(inDegrees);
double inDegrees = Math.toDegrees(inRadians);

Whenever we are using any of Java's trigonometric functions, we should first think about what is the unit of our input.

3. Using Math.sin

We can see this principle in action by taking a look at the Math.sin method, one of the many that Java provides:

public static double sin(double a)

It's equivalent to the mathematical sine function and it expects its input to be in radians. So, let's say that we have an angle we know to be in degrees:

double inDegrees = 30;

We first need to convert it to radians:

double inRadians = Math.toRadians(inDegrees);

And then we can calculate the sine value:

double sine = Math.sin(inRadians);

But, if we know it to already be in radians, then we don't need to do the conversion:

@Test
public void givenAnAngleInDegrees_whenUsingToRadians_thenResultIsInRadians() {
    double angleInDegrees = 30;
    double sinForDegrees = Math.sin(Math.toRadians(angleInDegrees)); // 0.5

    double thirtyDegreesInRadians = 1/6 * Math.PI;
    double sinForRadians = Math.sin(thirtyDegreesInRadians); // 0.5

    assertTrue(sinForDegrees == sinForRadians);
}

Since thirtyDegreesInRadians was already in radians, we didn't need to first convert it to get the same result.

4. Conclusion

In this quick article, we've reviewed radians and degrees and then saw an example of how to work with them using Math.sin.

As always, check out the source code for this example over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!