# Math Methods In Java

In this article, we will discuss the most commonly used Java Math methods, including the abs(), sqrt(), pow(), round(), min(), max(), floor(), and ceil() methods, and how to use them in your code.

We will explain each of these methods in detail, along with examples to help you understand their usage better.

## Java Math Methods

A list of all Java Math methods can be found in the table below:

 Methods Overview Return Type abs(mrx) This method displays the absolute value of mrx double|float|int|long acos(mrx) This function gives mrx’s arc cosine in radians double asin(mrx) The arcsine of mrx is displayed in radians double atan(mrx) An arctangent of mrx equal to -PI/2 or PI/2 is given as a numeric value double atan2(ample,mrx) Converts rectangular coordinates (mrx, ample) to polar coordinates (r, theta) and displays the angle theta. double cbrt(mrx) A Java Ref Math function used to find the cube root of mrx double ceil(mrx) A rounded up value of mrx to the nearest integer is given double copySign(mrx, ample) Specifies the sign of the second floating point ample and provides the first floating point mrx double cos(mrx) A cosine of mrx is calculated (mrx is in radians). double cosh(mrx) An expression that computes the hyperbolic cosine of a double datatype variable double exp(mrx) This function displays the value of Ex double expm1(mrx) Java Ref Math function expm1(mrx) displays ex -1 as the result double floor(mrx) This function outputs the rounded down value of mrx double getExponent(mrx) This function gives the unbiased exponent which was used in the mrx function int hypot(mrx, ample) Shows the result of sqrt(mrx + ample) without intermediate overflows or underflows double IEEEremainder(mrx, ample) Calculates the remainder operation on mrx and ample in accordance with IEEE 754 double log(mrx) A natural logarithm of mrx (base E) is calculated double log10(mrx) Based on Java Ref Math, this function calculates the base 10 logarithm of mrx double log1p(mrx) This function provides the natural logarithm (base E) of the sum of mrx and 1. double max(mrx, ample) A number with the highest value is displayed double|float|int|long min(mrx, ample) The number with the lowest value is presented double|float|int|long nextAfter(mrx, ample) It determines a floating point number in the direction of ample that is adjacent to the floating point number mrx double|float nextUp(mrx) Gives the positive infinity floating point value adjacent to mrx. double|float pow(mrx, ample) Gives the value of mrx raised to the power of ample double random() An integer between 0 and 1 is returned haphazardly double round(mrx) It displays the integer value of mrx rounded to the nearest whole number int rint() Calculates the closest double value to mrx that is also equal to a mathematical integer. double signum(mrx) Obtains the sign of mrx double sin(mrx) A Java Ref Math function that provides the sine of mrx (mrx is in radians) double sinh(mrx) A double value is transformed into its hyperbolic sine and is displayed double sqrt(mrx) A function that computes the square root of mrx double tan(mrx) A Java Reference Math function that gives the tangent of an angle double tanh(mrx) Computes the hyperbolic tangent of a double value double toDegrees(mrx) Converts an angle calculated in radians into an approximate angle. Measurement of equivalent angles in degrees. double toRadians(mrx) Angles measured in degrees are converted to approximate angles. The angle is measured in radians double ulp(mrx) This function gives the unit of least precision (ulp) for mrx double|float
Please note that all Java Math methods are static.

## Java Math Methods Math.max(mrx, ample)

Math.max(mrx, ample) can be used to find the highest value among two numbers:

#### Java Math Methods Math.max() Example: 1

public class Main { public static void main(String[] args) { int mrx=10; int ample=20;System.out.println("The highest value is: "+Math.max(mrx, ample)); } }

The following examples compares the age of two persons:

#### Java Math Methods Math.max() Example: 2

public class Main { public static void main(String[] args) { int mrx_age=15; int ample_age=21;System.out.println("The highest value is: "+Math.max(mrx_age, ample_age)); } }

## Java Math Methods Math.min(mrx, ample)

Math.max(mrx, ample) can be used to find the lowest value of x and ample:

#### Java Math Methods Math.min() Example: 1

public class Main { public static void main(String[] args) { int mrx=10; int ample=20;System.out.println("The minimum value among both is: "+Math.min(mrx, ample)); } }

#### Java Math Methods Math.min() Example: 2

public class Main { public static void main(String[] args) { int mrx_num=50; int ample_num=70;System.out.println("The minimum value among both is: "+Math.min(mrx_num,ample_num )); } }

The above example demonstrates the usage of the Math.min() method.

First, two integer variables mrx_num and ample_num are declared and initialized with the values 50 and 70, respectively.

Then, the Math.min() method is used to find the minimum value between the two numbers.

This method takes two parameters, and it returns the smaller value among them.

Finally, the result of Math.min(mrx_num,ample_num ) is passed as an argument to the System.out.println() method.

This method prints the given text and the calculated minimum value on the console.

## Java Math Methods Math.sqrt(mrx)

Math.sqrt(mrx) is a Java expression that calculates the square root of the value stored in the integer variable mrx using the sqrt() method of the Math class.

The sqrt() method is a built-in method in Java that returns the square root of a given number. It takes a single parameter, which is the value whose square root is to be calculated, and returns a double value that represents the square root of that value.

In the given expression, the value stored in the mrx variable is passed as an argument to the Math.sqrt() method. The method calculates the square root of mrx and returns the result as a double value.

It is worth noting that since mrx is an integer value, it will be automatically converted to a double value by Java before being passed as an argument to the sqrt() method. The result of Math.sqrt(mrx) will also be a double value.

#### Java Math Methods Math.sqrt() Example: 1

public class Main { public static void main(String[] args) { int mrx=81; System.out.println("The Square root of "+mrx+" is : "+Math.sqrt(mrx)); } }

We can also find the root of a number in decimals.

#### Java Math Methods Math.sqrt() Example: 2

public class Main { public static void main(String[] args) { int ample=141; System.out.println("The Square root of "+ample+" is : "+(double)Math.sqrt(ample)); } }

Above example calculates and prints the square root of an integer value.

First, an integer variable ample is declared and initialized with the value 141.

Then, the System.out.println() method is called to print a string message on the console.

The message is created by concatenating the text “The Square root of “, the value stored in the ample variable, and the text “is : “.

Next, the Math.sqrt() method is called with the ample variable as an argument.

This method calculates the square root of ample and returns a double value.

Since the Math.sqrt() method returns a double value, we need to cast it back to double explicitly to avoid a type mismatch error. Therefore, the Math.sqrt(ample) expression is enclosed in parentheses, and the entire expression is cast to double using the (double) operator.

Finally, the resulting double value is concatenated to the message using the + operator, and the entire string message is printed on the console.

## Random Numbers

The Math.random() function returns a random number between 0.0 (inclusive) and 1.0 (exclusive):

#### Math.random() Example: 1

public class Main { public static void main(String[] args) { System.out.println("Generating any number by the command: "+Math.random()); } }

#### Math.random() Example: 2

public class Main { public static void main(String[] args) { System.out.println("Generating any number Randomly: "+Math.random()); } }

Here’s a formula you can use if you want more control over the random number, such as a number between 0 and 200:

#### Math.random() Example: 3

public class Main { public static void main(String[] args) { int random_Num_Range = (int)(Math.random() * 201); // Any random number between 0 to 200 will be printed System.out.println(random_Num_Range); } }

Similarly to get a random number between 0 to 30 follow the example below:

#### Math.random() Example: 4

public class Main { public static void main(String[] args) { int random_Num_Range = (int)(Math.random() * 31); // Any random number between 0 to 30 will be printed System.out.println(random_Num_Range); } }

## Java Math Methods abs()

Java math abs() method is a built-in method in the java.lang.Math class in Java, used to find the absolute value of a given number.

The absolute value of a number is the magnitude of the number without regard to its sign.

The abs() method takes a single parameter, which can be of type int, long, float, or double, and returns the absolute value of that number as the same data type.

Numbers can be expressed as absolute values (positive values) using the abs() function.

### Syntax:

```public static double abs(double mrx)
public static float abs(float ample)
public static int abs(int mrx_1)
public static long abs(long mrx_ample)
```

The following example shows the absolute value of int, float and long variables:

#### Java Math Methods abs() Example: 1

public class Main { public static void main(String[] args) { System.out.println(Math.abs(-5.2)); System.out.println(Math.abs(3.2)); System.out.println(Math.abs(1)); } }

Similarly, this could also be done by assigning values to the variables such as:

#### Java Math Methods abs() Example: 2

public class Main { public static void main(String[] args) {long mrx=-333212312313L; float ample=-5.2321f;System.out.println("Orignal Value is: "+mrx+" after applying abs() method we get the result as: "+Math.abs(mrx)); System.out.println("Orignal float value: "+ample+" after applying abs() method we get the result: "+Math.abs(ample)); } }

The above example demonstrates the usage of the Math.abs() method to find the absolute value of a long and a float number.

First, a long variable mrx is declared and initialized with the value -333212312313L. Then, a float variable ample is declared and initialized with the value -5.2321f.

Next, the System.out.println() method is called to print two separate messages on the console.

The first message is created by concatenating the text “Orignal Value is: “, the value stored in the mrx variable, the text ” after applying abs() method we get the result as: “, and the result of Math.abs(mrx).

The Math.abs(mrx) expression calculates the absolute value of mrx using the abs() method of the Math class.

The second message is created by concatenating the text “Orignal float value: “, the value stored in the ample variable, the text ” after applying abs() method we get the result: “, and the result of Math.abs(ample).

The Math.abs(ample) expression calculates the absolute value of ample using the abs() method of the Math class.

## Java Math Methods acos()

Using the acos() method, you can calculate the arc cosine of a number.

Hint: This function returns the value of PI when acos(-1) is called.

The following program shows the calculation of value of cos inverse:

#### Java Math Methods acos() Example: 1

public class Main { public static void main(String[] args) {System.out.println("The value of cos inverse(0.88) is : "+Math.acos(0.88)); System.out.println("The value of cos inverse(-0.5) is : "+Math.acos(-0.5)); System.out.println("The value of cos inverse(-1) is : "+Math.acos(-1)+" which is the value of PI");} }

The given program shows the calculation of cos inverse, by taking the user input:

#### Java Math Methods acos() Example:2

import java.util.Scanner;public class Main { public static void main(String[] args) {Scanner my_input=new Scanner(System.in);System.out.println("Enter Any value between -1 to 1 : "); double mrx=my_input.nextDouble();System.out.println("\nThe value of cos inverse ( "+mrx+" )"+" is: "+Math.acos(mrx));} }

Above example prompts the user to input a value between -1 to 1 using the Scanner class from the java.util package.

1. The user’s input is then stored in a double variable mrx.
2. Next, the Math.acos() method is used to calculate the arc cosine of the mrx value.
3. The arc cosine function is an inverse trigonometric function that calculates the angle whose cosine is equal to the given value.
4. The result of the Math.acos() method is then printed on the console using the System.out.println() method.
5. The program begins by importing the java.util.Scanner package, which is used to get user input from the console.
6. In the main method, a new Scanner object is created and assigned to a variable named my_input.
7. The System.out.println() method is called to print the text “Enter Any value between -1 to 1 : ” on the console to prompt the user to input a value between -1 to 1.
8. The my_input.nextDouble() method is called to read the user’s input as a double value and store it in the mrx variable.
9. Then, the Math.acos(mrx) method is used to calculate the arc cosine of the mrx value.
10. The result of this calculation is then printed on the console using the System.out.println() method, which concatenates the text “The value of cos inverse ( “, the value stored in mrx, the text ” ) is: “, and the result of Math.acos(mrx).

When the program is executed, it will prompt the user to enter a value between -1 to 1. The program will then calculate the arc cosine of the user’s input using the Math.acos() method and print the result on the console.

For example, if the user enters the value 0.5, the program will output the text “The value of cos inverse ( 0.5 ) is: 1.0471975511965979”, which means that the arc cosine of 0.5 is 1.047 radians (or approximately 60 degrees).

You now have an understanding of what Java math methods are and how to use them.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0