Java Math in Java
Math in Java
Java provides a comprehensive Math class that includes a variety of static methods for performing mathematical operations such as basic arithmetic, trigonometric functions, logarithmic functions, and more. The methods in the Math class are part of the java.lang package, so you don’t need to import anything to use them.
Here’s an overview of the most commonly used methods in the Math class:
1. Arithmetic Functions
abs(double a): Returns the absolute value of a number.System.out.println(Math.abs(-10.5)); // 10.5max(double a, double b): Returns the larger of two numbers.System.out.println(Math.max(5, 10)); // 10min(double a, double b): Returns the smaller of two numbers.System.out.println(Math.min(5, 10)); // 5pow(double a, double b): Returnsaraised to the power ofb.System.out.println(Math.pow(2, 3)); // 8.0 (2^3)sqrt(double a): Returns the square root ofa.System.out.println(Math.sqrt(25)); // 5.0cbrt(double a): Returns the cube root ofa.System.out.println(Math.cbrt(27)); // 3.0round(double a): Rounds the floating-point number to the nearest integer.System.out.println(Math.round(5.6)); // 6System.out.println(Math.round(5.3)); // 5random(): Returns a random number between 0.0 (inclusive) and 1.0 (exclusive).System.out.println(Math.random()); // A random number between 0.0 and 1.0
2. Trigonometric Functions
sin(double a): Returns the sine of the anglea(in radians).System.out.println(Math.sin(Math.PI / 2)); // 1.0cos(double a): Returns the cosine of the anglea(in radians).System.out.println(Math.cos(Math.PI)); // -1.0tan(double a): Returns the tangent of the anglea(in radians).System.out.println(Math.tan(Math.PI / 4)); // 1.0asin(double a): Returns the arcsine ofa, in radians.System.out.println(Math.asin(1)); // 1.5708 (?/2)acos(double a): Returns the arccosine ofa, in radians.System.out.println(Math.acos(1)); // 0.0atan(double a): Returns the arctangent ofa, in radians.System.out.println(Math.atan(1)); // 0.7854 (?/4)toDegrees(double a): Converts an angle from radians to degrees.System.out.println(Math.toDegrees(Math.PI)); // 180.0toRadians(double a): Converts an angle from degrees to radians.System.out.println(Math.toRadians(180)); // 3.1416 (?)
3. Logarithmic and Exponential Functions
log(double a): Returns the natural logarithm (basee) ofa.System.out.println(Math.log(10)); // 2.302585log10(double a): Returns the base 10 logarithm ofa.System.out.println(Math.log10(100)); // 2.0exp(double a): Returnseraised to the power ofa.System.out.println(Math.exp(2)); // 7.389056expm1(double a): Returnse^a - 1.System.out.println(Math.expm1(1)); // 1.7182818
4. Hyperbolic Functions
sinh(double a): Returns the hyperbolic sine ofa.System.out.println(Math.sinh(1)); // 1.175201193643801cosh(double a): Returns the hyperbolic cosine ofa.System.out.println(Math.cosh(1)); // 1.5430806348152437tanh(double a): Returns the hyperbolic tangent ofa.System.out.println(Math.tanh(1)); // 0.7615941559557649
5. Miscellaneous Functions
PI: A constant for the value of Pi.System.out.println(Math.PI); // 3.141592653589793E: A constant for the value ofe(Euler's number).System.out.println(Math.E); // 2.718281828459045ulp(double a): Returns the size of the smallest adjacent floating-point number toa.System.out.println(Math.ulp(2.0)); // 4.44089209850063E-16
Example Usage
Here’s an example demonstrating various Math methods in Java:
public class MathExample { public static void main(String[] args) { // Arithmetic functions System.out.println("Absolute value of -5: " + Math.abs(-5)); // 5 System.out.println("Max of 5 and 10: " + Math.max(5, 10)); // 10 System.out.println("2 raised to the power 3: " + Math.pow(2, 3)); // 8.0 // Trigonometric functions System.out.println("Sine of 90 degrees: " + Math.sin(Math.toRadians(90))); // 1.0 System.out.println("Cosine of 0 degrees: " + Math.cos(Math.toRadians(0))); // 1.0 // Logarithmic functions System.out.println("Natural log of 10: " + Math.log(10)); // 2.302585 System.out.println("Base 10 log of 100: " + Math.log10(100)); // 2.0 // Random number System.out.println("Random number: " + Math.random()); // Hyperbolic functions System.out.println("Hyperbolic sine of 1: " + Math.sinh(1)); // 1.175201 }}Output:
Absolute value of -5: 5Max of 5 and 10: 102 raised to the power 3: 8.0Sine of 90 degrees: 1.0Cosine of 0 degrees: 1.0Natural log of 10: 2.302585Base 10 log of 100: 2.0Random number: 0.32843753456226585Hyperbolic sine of 1: 1.175201Conclusion
The Math class in Java provides an extensive set of static methods for performing mathematical calculations. From basic arithmetic to advanced functions like logarithms, trigonometry, and hyperbolic functions, Java's Math class can handle a wide range of mathematical tasks. Let me know if you'd like to dive deeper into any specific function or topic! ?