VMathematical Functions in Java
Java provides a wide range of mathematical functions through the Math class, which is part of the
java.lang package. These functions are very useful in solving complex calculations like trigonometric
operations, logarithmic operations, exponents, rounding values, and finding minimum or maximum
numbers. All functions are static, so they can be directly accessed using Math.functionName()
without creating an object.
1. Absolute Value Function
Method: Math.abs(x)
Description: Returns the absolute (positive) value of a number.
Example:
System.out.println(Math.abs(-25)); // Output: 25
System.out.println(Math.abs(12)); // Output: 12
2. Square Root Function
Method: Math.sqrt(x)
Description: Returns the positive square root of a number.
Example:
System.out.println(Math.sqrt(49)); // Output: 7.0
System.out.println(Math.sqrt(2)); // Output: 1.414213562
3. Power Function
Method: Math.pow(a, b)
Description: Returns the value of a raised to the power b.
Example:
System.out.println(Math.pow(2, 3)); // Output: 8.0
System.out.println(Math.pow(5, 0.5)); // Output: 2.2360679
4. Maximum and Minimum Functions
Methods:
o Math.max(a, b) → Returns the greater value.
o Math.min(a, b) → Returns the smaller value.
Example:
System.out.println(Math.max(10, 25)); // Output: 25
System.out.println(Math.min(10, 25)); // Output: 10
5. Rounding Functions
Methods:
o Math.round(x) → Rounds to the nearest integer.
o Math.floor(x) → Returns the largest integer less than or equal to the number.
o Math.ceil(x) → Returns the smallest integer greater than or equal to the number.
Example:
System.out.println(Math.round(7.5)); // Output: 8
System.out.println(Math.floor(7.9)); // Output: 7.0
System.out.println(Math.ceil(7.1)); // Output: 8.0
6. Trigonometric Functions
Methods:
o Math.sin(x) → Returns sine value.
o Math.cos(x) → Returns cosine value.
o Math.tan(x) → Returns tangent value.
o Math.asin(x) → Returns inverse sine (in radians).
o Math.acos(x) → Returns inverse cosine.
o Math.atan(x) → Returns inverse tangent.
Note: Values must be given in radians (not degrees). Use Math.toRadians(degree) for
conversion.
Example:
System.out.println(Math.sin(Math.toRadians(30))); // Output: 0.5
System.out.println(Math.cos(Math.toRadians(60))); // Output: 0.5
System.out.println(Math.tan(Math.toRadians(45))); // Output: 1.0
7. Logarithmic and Exponential Functions
Methods:
o Math.log(x) → Natural log (base e).
o Math.log10(x) → Logarithm (base 10).
o Math.exp(x) → Returns e raised to the power of x.
Example:
System.out.println(Math.log(2.71828)); // Output: ~1.0
System.out.println(Math.log10(100)); // Output: 2.0
System.out.println(Math.exp(2)); // Output: 7.389056
8. Random Number Function
Method: Math.random()
Description: Returns a random double value between 0.0 and 1.0.
Example:
System.out.println(Math.random()); // Output: random number
System.out.println((int)(Math.random()*10)); // Output: random integer 0–9
9. Conversion Functions
Methods:
o Math.toDegrees(x) → Converts radians to degrees.
o Math.toRadians(x) → Converts degrees to radians.
Example:
System.out.println(Math.toDegrees(Math.PI)); // Output: 180.0
System.out.println(Math.toRadians(90)); // Output: 1.570796
10. Special Constants
Constants in Math class:
o Math.PI → Value of π (3.141592653589793).
o Math.E → Base of natural logarithm (2.718281828).
Example:
System.out.println(Math.PI); // Output: 3.141592653589793
System.out.println(Math.E); // Output: 2.718281828
✨ Summary
Math.abs(), Math.sqrt(), Math.pow() → For absolute values, roots, and powers.
Math.max(), Math.min() → For comparison.
Math.round(), Math.ceil(), Math.floor() → For rounding.
Math.sin(), Math.cos(), Math.tan() → For trigonometry.
Math.log(), Math.exp() → For logarithms and exponents.
Math.random() → For random values.
Math.toDegrees(), Math.toRadians() → For unit conversions.
Constants like Math.PI and Math.E.