Java has had several advanced usage application including working with complex calculations in physics, architecture/designing of structures, working with Maps and corresponding latitudes/longitudes, etc.

In this tutorial, you will learn:

All such applications require using complex calculations/equations that are tedious to perform manually. Programmatically, such calculations would involve usage of logarithms, trigonometry, exponential equations, etc.

Now, you cannot have all the log or trigonometry tables hard-coded somewhere in your application or data. The data would be enormous and complex to maintain.

Java provides a very useful class for this purpose. It is the Math java class (java.lang.Math).

This class provides methods for performing the operations like exponential, logarithm, roots and trigonometric equations too.

Let us have a look at the methods provided by the Java Math class.

The two most fundamental elements in Math are the 'e' (base of the natural logarithm) and 'pi' (ratio of the circumference of a circle to its diameter). These two constants are often required in the above calculations/operations.

Hence the Math class java provides these two constants as double fields.

Math.E - having a value as 2.718281828459045

Math.PI - having a value as 3.141592653589793

A) Let us have a look at the table below that shows us the Basic methods and its description

 Method Description Arguments abs Returns the absolute value of the argument Double, float, int, long round Returns the closed int or long (as per the argument) double or float ceil Returns the smallest integer that is greater than or equal to the argument Double floor Returns the largest integer that is less than or equal to the argument Double min Returns the smallest of the two arguments Double, float, int, long max Returns the largest of the two arguments Double, float, int, long

Below is the code implementation of the above methods:

Note: There is no need to explicitly import java.lang.Math as its imported implicitly. All its methods are static.

Integer Variable

```int i1 = 27;
int i2 = -45;
```

Double(decimal) variables

```double d1 = 84.6;
double d2 = 0.45;
```

## Math.abs

```public class Guru99 {
public static void main(String args[]) {

int i1 = 27;
int i2 = -45;
double d1 = 84.6;
double d2 = 0.45;
System.out.println("Absolute value of i1: " + Math.abs(i1));

System.out.println("Absolute value of i2: " + Math.abs(i2));

System.out.println("Absolute value of d1: " + Math.abs(d1));

System.out.println("Absolute value of d2: " + Math.abs(d2));

}
}
```
Output:
```Absolute value of i1: 27
Absolute value of i2: 45
Absolute value of d1: 84.6
Absolute value of d2: 0.45```

## Math.round

```public class Guru99 {
public static void main(String args[]) {
double d1 = 84.6;
double d2 = 0.45;
System.out.println("Round off for d1: " + Math.round(d1));

System.out.println("Round off for d2: " + Math.round(d2));
}
}

```
Output:
```Round off for d1: 85
Round off for d2: 0```

## Math.ceil & Math.floor

```public class Guru99 {
public static void main(String args[]) {
double d1 = 84.6;
double d2 = 0.45;
System.out.println("Ceiling of '" + d1 + "' = " + Math.ceil(d1));

System.out.println("Floor of '" + d1 + "' = " + Math.floor(d1));

System.out.println("Ceiling of '" + d2 + "' = " + Math.ceil(d2));

System.out.println("Floor of '" + d2 + "' = " + Math.floor(d2));

}
}
```
Output:
```Ceiling of '84.6' = 85.0
Floor of '84.6' = 84.0
Ceiling of '0.45' = 1.0
Floor of '0.45' = 0.0
```

## Math.min

```public class Guru99 {
public static void main(String args[]) {
int i1 = 27;
int i2 = -45;
double d1 = 84.6;
double d2 = 0.45;
System.out.println("Minimum out of '" + i1 + "' and '" + i2 + "' = " + Math.min(i1, i2));

System.out.println("Maximum out of '" + i1 + "' and '" + i2 + "' = " + Math.max(i1, i2));

System.out.println("Minimum out of '" + d1 + "' and '" + d2 + "' = " + Math.min(d1, d2));

System.out.println("Maximum out of '" + d1 + "' and '" + d2 + "' = " + Math.max(d1, d2));

}
}
```
Output:
```Minimum out of '27' and '-45' = -45
Maximum out of '27' and '-45' = 27
Minimum out of '84.6' and '0.45' = 0.45
Maximum out of '84.6' and '0.45' = 84.6```

B) Let us have a look at the table below that shows us the Exponential and Logarithmic methods and its description-

 Method Description Arguments exp Returns the base of natural log (e) to the power of argument Double Log Returns the natural log of the argument double Pow Takes 2 arguments as input and returns the value of the first argument raised to the power of the second argument Double floor Returns the largest integer that is less than or equal to the argument Double Sqrt Returns the square root of the argument Double

Below is the code implementation of the above methods: (The same variables are used as above)

```public class Guru99 {
public static void main(String args[]) {
double d1 = 84.6;
double d2 = 0.45;
System.out.println("exp(" + d2 + ") = " + Math.exp(d2));

System.out.println("log(" + d2 + ") = " + Math.log(d2));

System.out.println("pow(5, 3) = " + Math.pow(5.0, 3.0));

System.out.println("sqrt(16) = " + Math.sqrt(16));

}
}
```
Output:
```exp(0.45) = 1.568312185490169
log(0.45) = -0.7985076962177716
pow(5, 3) = 125.0
sqrt(16) = 4.0
```

C) Let us have a look at the table below that shows us the Trigonometric methods and its description-

 Method Description Arguments Sin Returns the Sine of the specified argument Double Cos Returns the Cosine of the specified argument double Tan Returns the Tangent of the specified argument Double Atan2 Converts rectangular coordinates (x, y) to polar(r, theta) and returns theta Double toDegrees Converts the arguments to degrees Double Sqrt Returns the square root of the argument Double toRadians Converts the arguments to radians Double

Below is the code implementation:

```public class Guru99 {
public static void main(String args[]) {
double angle_30 = 30.0;

System.out.println("Theta = " + Math.atan2(4, 2));

}
}
```
Output:
```sin(30) = 0.49999999999999994
cos(30) = 0.8660254037844387
tan(30) = 0.5773502691896257
Theta = 1.1071487177940904```

Now, with the above, you can also design your own scientific calculator in java.

YOU MIGHT LIKE: