Java Math Class and Methods

Introduction

The java.math class contains methods that are used for geometric and trigonometric solutions. They also have methods for several general purposes. Math class defines two double constants: E(-2.72) and PI(-3.14)

Methods in Math Class

There are various methods found in Math class.

sin: The sin method has only one double value an as the parameter. sin() returns the trigonometric sine of an angle.

public static double sin (double a)

cos: The cos method has only one double value as the parameter. cos() returns the trigonometric cosine of an angle.

public static double cos (double a)

tan: The tan method has only one double value an as the parameter. tan () returns the trigonometric tangent of an angle.

public static double tan (double a)

The following program illustrates the use of the above methods.

import java.lang.*;
class SinEg{
public static void main(String args[])
{
double a=30;
double b=Math.toRadians(a);
System.out.println("The angle in radians is :" +b);
System.out.println("The Sine of the angle is :" +Math.sin(b));
System.out.println("The Cosine of the angle is :" +Math.cos(b));
System.out.println("The Tangent of the angle is :" +Math.tan(b));
}
}
  • Save the file as sinEg.java
  • Compile the file using javac sinEg.java
  • Run the file using Java sinEg

Output

java.math class

asin, acos, atan method

asin: The asin method has only one double value a (whose are sine to be returned) as the parameter. This method returns the arc sine of an angle in the range of –pi/2 through pi/2.

public static double asin(double a)

acos: The acos() has only one double value an as the parameter. acos() returns the arc cosine of an angle in the range of zero. Zero through pi.

public static double acos(double a)

atan: The atan method has only one double value a(whose arc tangent is to be returned) as the parameter. This method returns the arc tangent of an angle, the range –pi/2 through pi/2.

The following code illustrates the use of the above methods:

import java.lang.*;
class aTrig
{
public static void main(String args[])
{
double s= .5;
System.out.println("The input value is ................" +s);
System.out.println("The angle for which the sine value is 0.5 is :" +Math.asin(s));
System.out.println("The cosine of the angle is :"+Math.acos(s));
System.out.println("The cosine of the angle is :"+Math.atan(s));
}
}

Output

java.math class

toRadians, toDegrees Method

toRadians

public static double toRadians(double ang)

The toRadians method has only one double value as the parameters. The toRadians() converts an angle measured in degrees to the equivalent angle measured in radians.

toDegrees

 public static double toDegrees(double angr)

The toDegrees() has only one double value angr as the parameter. This method returns the measurements of the angle angr in degrees.

The following example illustrates the use of the above methods.

import java.math.*;
class math1
{
public static void main(String args[])
{
System.out.println("The Radian equivalent of 90 degree:"+Math.toRadians(90));
System.out.println("The Degree equivalent of 1.57 radians:"+Math.toDegrees(1.571));
System.out.println("\n"+"The atan2 value of two parameters:"+Math.atan2(30,30));
System.out.println("\n"+"The log value of 30:"+Math.log(30));
System.out.println("\n"+"The exponent value of 30:"+Math.exp(30));
}
}
  • Save the file as Math1.java
  • Compile the file using javac Math1.java
  • Run the file using Java Math1

Output

java.math class

Summary

The java.math class contains methods that are used for geometric and trigonometric solutions. This asin() returns the arc sine of an angle in the range of –pi/2 through pi/2. toRadians method converts an angle measured in degrees to the equivalent angle measured in radians.


Similar Articles