Definition about Basic Math Functions.

Java, which is an important programming language, is a popular program computer in the world. Java is also used for: mobile applications (especially Android apps; desktop applications; web applications; web servers and application servers; database connection; and much more. Above the key information of Java, we have the answer for why we use Java. 

Moreover, the java.lang.Math includes a package of the functions of basic math that obtains the exact, random and rounding value. It also can be the highest, lowest from two values. Thus, the basic operation of math in Java will be enclosed in the explanations below.

What are Basic Math Functions in Java?

  1. Math.abs()

The function of Math.abs() get back the exact passed value. This exact value is the parameter’s positive pack. If it is negative, their symbol is uninstalled, and the positive one corresponding to an unfit value without the symbol is returned. So, for example are 2 Math.abs() methods:

int abs1 = Math.abs(10);  // abs1 = 10
int abs2 = Math.abs(-20); // abs2 = 20

The exact value of  figure 10 will be 10 and similar with 20 will be 20. Therefore, the method of  Math.abs()  can be overworked in these versions such as long, int, float and double as follow:

Math.abs(int)
Math.abs(long)
Math.abs(float)
Math.abs(double)

2. Math.ceil()

To understand Math.ceil() Java, below is an evident. Its function for the rounded profit and got back by a double:

double ceil = Math.ceil(7.343);  // ceil = 8.0

After running the code of Java, the ceil will hold the value of 8.0.

3. Math.floor()

Opposite with  Math.ceil(), The Math.floor() get back the double that it is the figure rounded down to the nearest integer value:

double floor = Math.floor(7.343);  // floor = 7.0

It is hold the value of 8.0 after running this code in Java

4. Math.floorDiv()

The method of Math.floorDiv() split up one int or long integer and the nearest figure value will be rounds. When the outcome is not negative, the effect is like using / division operator in Java. However, it is negative, the result is different. Below is example of Math.floorDiv() in Java:

double result3 = Math.floorDiv(-100,9);
System.out.println("result3: " + result3);
 
double result4 = -100 / 9;
System.out.println("result4: " + result4);

The output example in Java:

Command Prompt
result3: -12.0
result4: -11.0

From this code you know the difference  Math.floorDiv()  with the / division.

5. Math.min()

Here is an example for the Math.min() method which gets back the smallest of two values and parameter passed to it. After running this command, the small variable would hold the value of 10.

int min = Math.min(10, 20);

6. Math.max()

The method of Math.max() gets back the largest two passed values passed to parameter. After running this command, the biggest variable holds the value of 20.

int max = Math.max(10, 20);

7. Math.round()

The method of  Math.round()  gives the float, or double to the nearest integer that can be using normal rules of math rounds. Here is an example:

double roundedDown = Math.round(23.445);
double roundedUp   = Math.round(23.545);

8. Math.random()

The method of Math.random() gets back a floating figure randomly between 0 and 1. The result will be unpredictable also:

double random = Math.random();

Example to run random figure if it is value 100

double random = Math.random() * 100D;

When you prefer a value of integer , please use this method of round()floor() or ceil().

Exponential and Logarithmic Math FunctionsThe Math in Java also includes a package of functions intended, it is calculated for exponential and logarithmic.

9. Math.abs()

The function of Math.abs() get back the exact passed value. This exact value is the parameter’s positive pack. If it is negative, their symbol is uninstalled, and the positive one corresponding to an unfit value without the symbol is returned. For example are 2 Math.abs() methods:

int abs1 = Math.abs(10);  // abs1 = 10
int abs2 = Math.abs(-20); // abs2 = 20

The exact value of  figure 10 will be 10 and similar with 20 will be 20.The method of  Math.abs()  can be overworked in these versions such as long, int, float and double as follow:

Math.abs(int)
Math.abs(long)
Math.abs(float)
Math.abs(double)

10. Math.ceil()

To understand Math.ceil() Java, below is an evident. Its function for the rounded profit and got back by a double:

double ceil = Math.ceil(7.343);  // ceil = 8.0

After running the code of Java, the ceil will hold the value of 8.0.

11. Math.floor()

Opposite with  Math.ceil(), The Math.floor() get back the double that it is the figure rounded down to the nearest integer value:

double floor = Math.floor(7.343);  // floor = 7.0

It is hold the value of 8.0 after running this code in Java

12. Math.floorDiv()

The method of Math.floorDiv() split up one int or long integer and the nearest figure value will be rounds. When the outcome is not negative, the effect is like using / division operator in Java. However, it is negative, the result is different. Below is example of Math.floorDiv() in Java:

double result3 = Math.floorDiv(-100,9);
System.out.println("result3: " + result3);
 
double result4 = -100 / 9;
System.out.println("result4: " + result4);

The output example in Java:

Command Prompt
result3: -12.0
result4: -11.0

From this code you know the difference  Math.floorDiv()  with the / division.

13. Math.min()

Here is an example for the Math.min() method which gets back the smallest of two values and parameter passed to it. After running this command, the small variable would hold the value of 10.

int min = Math.min(10, 20);

14. Math.max()

The method of Math.max() gets back the largest two passed values passed to parameter. After running this command, the biggest variable holds the value of 20.

int max = Math.max(10, 20);

15. Math.round()

The method of  Math.round()  gives the float, or double to the nearest integer that can be using normal rules of math rounds. Here is an example:

double roundedDown = Math.round(23.445);
double roundedUp   = Math.round(23.545);

When importing these two statements in Java, the roundedDown variable would cover the value of 23.0 , and value of 24.0 would be the result by  the roundedUp.

16. Math.random()

The method of Math.random() gets back a floating figure randomly between 0 and 1. The result will be unpredictable also:

double random = Math.random();

Example to run random figure if it is value 100

double random = Math.random() * 100D;

When you prefer a value of integer , please use this method of round()floor() or ceil().

The math function with exponential and logarithmic value

The Math in Java also includes a package of functions intended, it is calculated for exponential and logarithmic. 

17. Math.exp()

The function of  Math.exp() gets back  e (Euler’s figure) up to the value’s power covered as parameter. See this example:

double exp1 = Math.exp(1);
System.out.println("exp1 = " + exp1);
 
double exp2 = Math.exp(2);
System.out.println("exp2 = " + exp2);

If the code and Java is matching, the output will display:

Command Prompt
exp1 = 2.718281828459045
exp2 = 7.38905609893065

18. Math.log()

The method of Math.log() shows the logarithm which is in the showed parameter, is i (Euler’s figure). So, the function of Math.exp() is provided by Math.log(). Here is the code:

double log1  = Math.log(1);
System.out.println("log1 = " + log1);
 
double log10 = Math.log(10);
System.out.println("log10 = " + log10);

Example for Math.log() output is:

Command Prompt
log1 = 0.0
log10 = 2.302585092994046

19. Math.log10()

The method of Math.log10 and Math.log()  run the same except is uses 10 of the logarithm instead of e (Euler’s figure). Example of  Math.log10()  in Java:

double log10_1   = Math.log10(1);
System.out.println("log10_1 = " + log10_1);
 
double log10_100 = Math.log10(100);
System.out.println("log10_100 = " + log10_100);

The output of Math.log10()  in Java:

Command Prompt
log10_1 = 0.0
log10_100 = 2.0

20. Math.pow()

The function of Math.pow() which gets two parameters and gets back the value of the first parameter up to  the second. So, we will provide the example for this Math.pow():

double pow2 = Math.pow(2,2);
System.out.println("pow2 = " + pow2);
 
double pow8 = Math.pow(2,8);
System.out.println("pow8 = " + pow8);

On the other hand, The value of 22 and 28 calculated in the Math.pow() would be 4 and 256. The code of this Math.pow()’s output is as:

Command Prompt
pow2 = 4.0
pow8 = 256.0

21. Math.sqrt()

The method of Math.sqrt() give the square root of the given parameter below:

double sqrt4 = Math.sqrt(4);
System.out.println("sqrt4 = " + sqrt4);
 
double sqrt9 = Math.sqrt(9);
System.out.println("sqrt9 = " + sqrt9);

The output of these Java Math.sqrt() examples is:

Output:

Command Prompt
sqrt4 = 2.0
sqrt9 = 3.0

The functions of Trigonometric Math 

The Math class in Java includes a set of the functions of trigonometric functions such as sine, cosine, tangens.  

22. Math.PI

The Math.PI  is considered as constant with a value  nearest to the value of PI – the mathematical content of PI.

23. Math.sin()

The method of Math.sin() c gives the sine value of some angle value in radians:

uble sin = Math.sin(Math.PI);
System.out.println("sin = " + sin);

24. Math.cos()

The method of Math.cos() gives the cosine value of some angle value in radians:

double cos = Math.cos(Math.PI);
System.out.println("cos = " + cos);

25. Math.tan()

The method of Math.tan()  gives the value of tangens in radians as follow:

double tan = Math.tan(Math.PI);
System.out.println("tan = " + tan);

26. Math.asin()

The method of Math.asin()  gives the value of arc sine between 1 and -1.

double asin = Math.asin(1.0);
System.out.println("asin = " + asin);

27. Math.acos()

The method of Math.acos()  gives the value of arc cosine between 1 and -1 below:

double acos = Math.acos(1.0);
System.out.println("acos = " + acos);

28. Math.atan()

The method of Math.atan()  gives the value of arc tangen  as between 1 and -1.

double atan = Math.atan(1.0);
System.out.println("atan = " + atan);

29. Math.atan2()

Below is the method of JavaDoc that we will explain for you by the following example:

double atan = Math.atan(1.0);
System.out.println("atan = " + atan);

30. Math.sinh()

The method of Math.sinh()  gives the value of hyperbolic sine between 1 and -1. Thus, example for Math.sinh() as:

double sinh = Math.sinh(1.0);
System.out.println("sinh = " + sinh);

31. Math.cosh()

The method of Math.cosh()  gives the value of hyperbolic cosine between 1 and -1. Here is an example:

double cosh = Math.cosh(1.0);
System.out.println("cosh = " + cosh);

32. Math.tanh()

The method of Math.tanh() gives the hyperbolic tangens with the value between 1 and -1. So, here is an example of Math.tanh() in Java:

double tanh = Math.tanh(1.0);
System.out.println("tanh = " + tanh);

33. Math.toDegrees()

The method of Math.toDegrees() gives an angle in radians to degrees. Example of Math.toDegrees():

double degrees = Math.toDegrees(Math.PI);
System.out.println("degrees = " + degrees);

34. Math.toRadians()

The method of Math.toRadians() gives an angle in degrees to radians. Example:

ouble radians = Math.toRadians(180);
System.out.println("radians = " + radians);

Conclusion 

Finally, we hope our post helps you have the knowledge of Basic Math Functions throughout the example and output of each case. If you have any questions related to the function of basic math in Java, please let us know by the comment box. Thank you.

Scroll to Top