Java Math Methods in Java
Math Methods in Java
The Math class in Java provides a wide range of methods for performing various mathematical operations. These methods are static, meaning you can call them directly using the class name without creating an instance of Math. Below is a breakdown of the commonly used Math methods in Java:
1. Arithmetic Methods
abs(double a): Returns the absolute value ofa.Math.abs(-5.5); // Returns 5.5max(double a, double b): Returns the maximum of two numbers.Math.max(10, 20); // Returns 20min(double a, double b): Returns the minimum of two numbers.Math.min(10, 20); // Returns 10pow(double a, double b): Returnsaraised to the power ofb.Math.pow(2, 3); // Returns 8.0 (2^3)sqrt(double a): Returns the square root ofa.Math.sqrt(16); // Returns 4.0cbrt(double a): Returns the cube root ofa.Math.cbrt(27); // Returns 3.0round(double a): Returns the closest long toa, rounding up if necessary.Math.round(3.5); // Returns 4random(): Returns a random number between0.0(inclusive) and1.0(exclusive).Math.random(); // Returns a random number between 0.0 and 1.0
2. Trigonometric Methods
sin(double a): Returns the sine of anglea(in radians).Math.sin(Math.PI / 2); // Returns 1.0cos(double a): Returns the cosine of anglea(in radians).Math.cos(Math.PI); // Returns -1.0tan(double a): Returns the tangent of anglea(in radians).Math.tan(Math.PI / 4); // Returns 1.0asin(double a): Returns the arcsine ofa(in radians).Math.asin(1); // Returns 1.5708 (?/2)acos(double a): Returns the arccosine ofa(in radians).Math.acos(1); // Returns 0.0atan(double a): Returns the arctangent ofa(in radians).Math.atan(1); // Returns 0.7854 (?/4)toDegrees(double a): Converts an angle from radians to degrees.Math.toDegrees(Math.PI); // Returns 180.0toRadians(double a): Converts an angle from degrees to radians.Math.toRadians(180); // Returns 3.1416 (?)
3. Logarithmic Methods
log(double a): Returns the natural logarithm (basee) ofa.Math.log(10); // Returns 2.302585log10(double a): Returns the base 10 logarithm ofa.Math.log10(100); // Returns 2.0exp(double a): Returnseraised to the power ofa.Math.exp(2); // Returns 7.389056expm1(double a): Returnse^a - 1.Math.expm1(1); // Returns 1.7182818
4. Hyperbolic Methods
sinh(double a): Returns the hyperbolic sine ofa.Math.sinh(1); // Returns 1.175201cosh(double a): Returns the hyperbolic cosine ofa.Math.cosh(1); // Returns 1.5430806tanh(double a): Returns the hyperbolic tangent ofa.Math.tanh(1); // Returns 0.761594
5. Miscellaneous Methods
PI: A constant for the value of Pi (?).Math.PI; // Returns 3.141592653589793E: A constant for the value of Euler's number (e).Math.E; // Returns 2.718281828459045ulp(double a): Returns the size of the smallest adjacent floating-point number toa.Math.ulp(2.0); // Returns 4.44089209850063E-16
Example Program Using Math Methods
Here is a sample program that demonstrates the usage of various Math methods:
public class MathMethodsExample { public static void main(String[] args) { // Arithmetic Methods System.out.println("Absolute value of -10.5: " + Math.abs(-10.5)); // 10.5 System.out.println("Max of 5 and 10: " + Math.max(5, 10)); // 10 System.out.println("2 raised to the power of 3: " + Math.pow(2, 3)); // 8.0 // Trigonometric Methods 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 Methods 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 Method System.out.println("Random number: " + Math.random()); // Hyperbolic Methods System.out.println("Hyperbolic sine of 1: " + Math.sinh(1)); // 1.175201 }}Sample Output:
Absolute value of -10.5: 10.5Max of 5 and 10: 102 raised to the power of 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.2747473649370256Hyperbolic sine of 1: 1.175201Conclusion
The Math class in Java offers a comprehensive set of static methods to perform a wide range of mathematical calculations, from basic arithmetic and trigonometry to logarithmic and hyperbolic operations. These methods are highly efficient and make it easy to implement mathematical logic in your programs.
Let me know if you need further clarification or examples! ?