Math Functions in Java
Java is one of the most useful programming languages. It has a variety of applications such as architecture building, solving calculations in science, building maps, etc. To make these tasks easy, Java provides a java.lang.Math class or Math Functions in Java that performs several operations such as square, exponential, ceil, logarithm, cube, abs, trigonometry, square root, floor, etc.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
This class provides two fields which are the basics of math class. They are,
- ‘e’ which is the natural logarithm’s base(718281828459045)
- ‘pi’ which is the ratio of the circumference of a circle to its diameter(141592653589793)
Various Math Functions in Java
Java offers a plethora of Math methods. They can be classified as shown below:
- Basic Math Methods
- Trigonometric Math Methods
- Logarithmic Math Methods
- Hyperbolic Math Methods
- Angular Math Methods
Now, let us look into them in detail.
1. Basic Math Methods
For better understanding, we can implement the above methods in a Java program as shown below:
Method | Return value | Arguments |
Example |
abs() |
Argument’s absolute value. i.e. positive value | long, int, float, double |
int n1 = Math.abs (80) //n1=80 int n2 =Math.abs (-60) //n2=60 |
sqrt() |
The square root of the argument | double |
double n= Math.sqrt (36.0) // n=6.0 |
cbrt() |
Cube root of the argument | double |
double n= Math.cbrt (8.0) // n=2.0 |
max() |
Maximum of the two values passed in the argument | long, int, float, double |
int n=Math.max(15,80) //n=80 |
min() |
Minimum of the two values passed in the argument | long, int, float, double |
int n=Math.min(15,80) //n=15 |
ceil() |
Rounds float value up to an integer value | double | double n=Math.ceil(6.34) //n=7.0 |
floor() | Rounds float value down to an integer value | double |
double n=Math.floor(6.34) //n=6.0 |
round() |
Rounds the float or double value to an integer value either up or down | double, float | double n = Math.round(22.445);//n=22.0 double n2 = Math.round(22.545); //n=23.0 |
pow() |
Value of the first parameter raised to the second parameter |
double |
double n= Math.pow(2.0, 3.0) //n=8.0 |
random() |
A random number between 0 and 1 | double | double n= Math.random() //n= 0.2594036953954201 |
signum() |
Sign of the passed parameter.
If positive, 1 will be displayed. If negative, -1 will be displayed. If 0, 0 will be displayed |
double, float |
double n = Math. signum (22.4);//n=1.0 double n2 = Math. signum (-22.5);//n=-1.0 |
addExact() |
Sum of the parameters. Exception is thrown if the result obtained overflows long or int value. | int, long |
int n= Math.addExact(35, 21)//n=56 |
incrementExact() |
Parameter incremented by 1. The exception is thrown if the result obtained overflows int value. | int, long |
int n=Math. incrementExact(36) //n=37 |
subtractExact() |
Difference of the parameters. The exception is thrown if the result obtained overflows int value. | int, long |
int n= Math.subtractExact(36, 11) //n=25 |
multiplyExact() |
Sum of the parameters. Exception is thrown if the result obtained overflows long or int value. | int, long |
int n= Math.multiplyExact(5, 5) //n=25 |
decrementExact() |
Parameter decremented by 1. The exception is thrown if the result obtained overflows int or long value. | int, long |
int n=Math. decrementExact (36) //n=35 |
negateExact() |
The negation of the parameter. The exception is thrown if the result obtained overflows int or long value. | int, long |
int n=Math. negateExact(36) //n=-36 |
copySign() |
Absolute value of the first parameter along with the sign specified in the second parameters | double,float |
double d= Math.copySign(29.3,-17.0) //n=-29.3 |
floorDiv() |
Divide the first parameter by second parameter and floor operation is performed. | long, int |
int n= Math.floorDiv(25, 3) //n=8 |
hypot() |
the sum of the squares of the parameters and perform square root operation. Intermediate overflow or underflow should not be there. | double |
double n=Math.hypot(4,3) //n=5.0 |
getExponent() |
unbiased exponent. This exponent is represented in double or float | int |
double n=Math.getExponent(50.45) //n=5 |
Code:
//Java program to implement basic math functions public class JavaMathFunctions { public static void main(String[] args) { int n1 = Math.abs(80); System.out.println("absolute value of 80 is: "+n1); int n2 = Math.abs(-60); System.out.println("absolute value of -60 is: "+n2); double n3 = Math.sqrt(36.0); System.out.println("Square root of 36.0 is: "+n3); double n4 = Math.cbrt(8.0); System.out.println("cube root 0f 8.0 is: "+n4); int n5= Math.max(15,80); System.out.println("max value is: "+n5); int n6 =Math.min(15,80); System.out.println("min value is: "+n6); double n7 = Math.ceil(6.34); System.out.println("ceil value of 6.34 is "+n7); double n8 = Math.floor(6.34); System.out.println("floor value of 6.34 is: "+n8); double n9 = Math.round(22.445); System.out.println("round value of 22.445 is: "+n9); double n10 = Math.round(22.545); System.out.println("round value of 22.545 is: "+n10); double n11= Math.pow(2.0, 3.0); System.out.println("power value is: "+n11); double n12= Math.random(); System.out.println("random value is: "+n12); double n13 = Math. signum (22.4); System.out.println("signum value of 22.4 is: "+n13); double n14 = Math. signum (-22.5); System.out.println("signum value of 22.5 is: "+n14); int n15= Math.addExact(35, 21); System.out.println("added value is: "+n15); int n16=Math. incrementExact(36); System.out.println("increment of 36 is: "+n16); int n17 = Math.subtractExact(36, 11); System.out.println("difference is: "+n17); int n18 = Math.multiplyExact(5, 5); System.out.println("product is: "+n18); int n19 =Math. decrementExact (36); System.out.println("decrement of 36 is: "+n19); int n20 =Math. negateExact(36); System.out.println("negation value of 36 is: "+n20); } }
Output:
2. Trigonometric Math Methods
Following is the Java program to implement trigonometric math functions mentioned in the table:
|
Return value | Arguments | Example | ||||||||||||||||||||||||||||
sin() |
Sine value of the parameter | double |
double num1 = 60; //Conversion of value to radians double value = Math.toRadians(num1); print Math.sine (value) //output is 0.8660254037844386 |
||||||||||||||||||||||||||||
cos() |
Cosine value of the parameter | double |
double num1 = 60; //Conversion of value to radians double value = Math.toRadians(num1); print Math.cos (value) //output is 0.5000000000000001 |
||||||||||||||||||||||||||||
tan() |
tangent value of the parameter | double |
double num1 = 60; //Conversion of value to radians double value = Math.toRadians(num1); print Math.tan(value) //output is 1.7320508075688767 |
||||||||||||||||||||||||||||
asin() |
Arc Sine value of the parameter. Or Inverse sine value of the parameter | double |
Math.asin(1.0) // 1.5707963267948966 |
||||||||||||||||||||||||||||
acos() |
Arc cosine value of the parameter Or Inverse Cosine value of the parameter | double |
Math.acos(1.0) //0.0 |
||||||||||||||||||||||||||||
atan() |
Arctangent value of the parameter Or Inverse tangent value of the parameter | double |
Math.atan(6.267) // 1.4125642791467878 |
Code:
//Java program to implement trigonometric math functions public class JavaMathFunctions { public static void main(String[] args) { double num1 = 60; // Conversion of value to radians double value = Math.toRadians(num1); System.out.println("sine value is : "+Math.sin(value)); System.out.println("cosine value is : "+Math.cos(value)); System.out.println("tangent value is : "+Math.tan(value)); double num2 = 1.0; System.out.println("acosine value is : "+Math.acos(num2)); System.out.println("asine value is : "+Math.asin(num2)); double num3 = 6.267; System.out.println("atangent value is : "+Math.atan(num3)); <strong>Output:</strong>
3. Logarithmic Math Methods
Following is the sample program that implements Logarithmic math methods:
Method |
Return Value | Arguments |
Example |
expm1() |
Calculate E’s power and minus 1 from it. E is Euler’s number. So here, it is ex-1. | double |
double n = Math.expm1(2.0) // n = 6.38905609893065 |
exp() |
E’s power to the given parameter. That is, ex | double |
double n=Math.exp(2.0) //n = 7.38905609893065 |
log() |
Natural logarithm of parameter | double |
double n=Math.log(38.9) //n=3.6609942506244004 |
log10() |
Base 10 logarithm of parameter | double |
double n = Math.log10(38.9) //n= 1.5899496013257077 |
log1p() |
Natural logarithm of the sum of parameter and one. ln(x+1) | double |
double n = Math.log1p(26) //n= 3.295836866004329 |
Code:
//Java program to implement logarithmic math functions public class JavaMathFunctions { public static void main(String[] args) { double n1 = Math.expm1(2.0); double n2 = Math.exp(2.0); double n3 = Math.log(38.9); double n4 = Math.log10(38.9); double n5 = Math.log1p(26); System.out.println("expm1 value of 2.0 is : "+n1); System.out.println("exp value of 2.0 is : "+n2); System.out.println("log of 38.9 is : "+n3); System.out.println("log10 of 38.9 is : "+n4); System.out.println("log1p of 26 is : "+n5); }}
Output:
4. Hyperbolic Math Methods
Following is the Java program to implement hyperbolic math functions mentioned in the table:
Method |
Return value | Arguments |
Example |
sinh() |
Hyperbolic Sine value of the parameter. i.e (ex – e -x)/2 Here, E is the Euler’s number. | double |
double num1=Math.sinh (30) //output is 5.343237290762231E12 |
cosh() |
Hyperbolic Cosine value of the parameter. i.e. (ex + e -x)/2 Here, E is the Euler’s number. | double |
double num1 = Math.cosh (60.0) //output is 5.710036949078421E25 |
tanh() |
Hyperbolic tangent value of the parameter | double |
double num1= Math.tanh (60.0) //output is 1.0 |
Code:
//Java program to implement HYPERBOLIC math functions public class JavaMathFunctions { public static void main(String[] args) { double n1 = Math.sinh (30); double n2 = Math.cosh (60.0); double n3 = Math.tanh (60.0); System.out.println("Hyperbolic sine value of 300 is : "+n1); System.out.println("Hyperbolic cosine value of 60.0 is : "+n2); System.out.println("Hyperbolic tangent value of 60.0 is : "+n3); } }
Output:
5. Angular Math Methods
Method | Return Value | Arguments | Example |
toRadians() | Degree angle converts to radian angle | double |
double n = Math.toRadians(180.0) //n= 3.141592653589793 |
toDegrees() | Radian angle converts to Degree angle | double |
double n = Math. toDegrees (Math.PI) //n=180.0 |
Now, let us see a sample program to demonstrate Angular Math methods.
Code:
//Java program to implement Angular math functions public class JavaMathFunctions { public static void main(String[] args) { double n1 = Math.toRadians(180.0); double n2 = Math. toDegrees (Math.PI); System.out.println("Radian value of 180.0 is : "+n1); System.out.println("Degree value of pi is : "+n2); } }
Output:
Conclusion
Java offers a wide variety of math functions to perform different tasks such as scientific calculations, architecture designing, structure designing, building maps, etc. This document discusses several basic, trigonometric, logarithmic and angular math functions in detail with sample programs and examples.
The above is the detailed content of Math Functions in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
