User Brian
Level 41

Math.cos() method in Java

Published in the Java Developer group
Math class in Java contains a lot of mathematical functions. Trigonometric functions are among the most important functions for programming. One of these functions is Math.cos().

Trigonometry for programming?

Of course, there are programmers who almost never come across trigonometric functions in their work, but nevertheless, for many tasks, these functions are extremely important. For example, for computer graphics or game logic. In particular, sines and cosines are involved in the so-called rotation matrix you can use to rotate objects and worlds. And if you need to calculate the length of the path along the map, trigonometric functions can come in handy.

Math.cos() method in Java

The double cos (double x) method of the Math class returns the cosine value of the x, where x is an argument, an angle in radians. Here is a declaration of the Java.lang.Math.cos() Method:

double cos(double x)
If you are uncomfortable with calculating angles in radians, you can use the special function to convert radians to degrees:

double toDegrees(double angRad)
There is also an inverse function that converts degrees to radians, which can also be useful.

double toRadians(double angDeg)
Here is a code example of java.lang.Math.cos():

public class CosExample {
   public static void main(String[] args) {
       int x1 = 1;
       double x2 = 0.5;
       double x3 = Math.PI;

       //using java.lang.Math.cos() for 1, 0.5 and PI rad 

       System.out.println("cosine of " + x1 + " rads = " + Math.cos(x1));
       System.out.println("cosine of  " + x2 + " rads = " + Math.cos(0));
       System.out.println("cosine  " + x3 + " rads = " + Math.exp(x3));

       //here we declare an 60 degrees angle

       double degree = 60;
       //here we use Math.toRadians to convert 60 degrees to radians, use the cos() method
       //to calculate the cosine of 60 degrees angle and print the result out
       System.out.println("cosine of " + degree + " degrees = " + Math.cos(Math.toRadians(degree)));

The output is:
cosine of 1 rads = 0.5403023058681398 cosine of 0.5 rads = 1.0 cosine 3.141592653589793 rads = 23.140692632779267 cosine of 60.0 degrees = 0.5000000000000001

Some special cases

In math there are concepts of indeterminate form, positive and negative infinity. A number divided by 0.0 gives infinity, positive or negative depending on the positivity or negativity of that number. You can get indeterminate form in different ways. For example, if you try to divide zero by zero or infinity to infinity. In Java there are special constants from class Double such as Double.NaN (not a number, you may say it’s a kind of indeterminate form), Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY. Math.cos() method behaves in a specific way when faced with these three concepts. If the argument is NaN or an infinity, then Math.cos() is NaN. Let’s have a code example:

public class CosSpecialCases {

       public static void main(String[] args) {

           double positiveInfinity = Double.POSITIVE_INFINITY;
           double negativeInfinity = Double.NEGATIVE_INFINITY;
           double nan = Double.NaN;

           //The argument is positive infinity, the output is NaN

           //The argument is negative infinity, the output NaN

           //The argument is NaN, the output is NaN
The output is:

A sine and cosine task for beginners

Try to program the movement of the clock hands using Math.cos() and Math.sin(). You can also attach graphics (using Processing, JavaFX or something else) to this task and you will get an animated clock. Math.cos() method in Java - 1