Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc. Show Unlike some of the StrictMath class numeric methods, all implementations of the equivalent function of Math class can't define to return the bit-for-bit same results. This relaxation permits implementation with better-performance where strict reproducibility is not required. If the size is int or long and the results overflow the range of value, the methods addExact(),�subtractExact(),�multiplyExact(), and�toIntExact()�throw an�ArithmeticException. For other arithmetic operations like increment, decrement, divide, absolute value, and negation overflow�occur only with a specific minimum or maximum value. It should be checked against the maximum and minimum value as appropriate. Example 1public class JavaMathExample1 { public static void main(String[] args) { double x = 28; double y = 4; // return the maximum of two numbers System.out.println("Maximum number of x and y is: " +Math.max(x, y)); // return the square root of y System.out.println("Square root of y is: " + Math.sqrt(y)); //returns 28 power of 4 i.e. 28*28*28*28 System.out.println("Power of x and y is: " + Math.pow(x, y)); // return the logarithm of given value System.out.println("Logarithm of x is: " + Math.log(x)); System.out.println("Logarithm of y is: " + Math.log(y)); // return the logarithm of given value when base is 10 System.out.println("log10 of x is: " + Math.log10(x)); System.out.println("log10 of y is: " + Math.log10(y)); // return the log of x + 1 System.out.println("log1p of x is: " +Math.log1p(x)); // return a power of 2 System.out.println("exp of a is: " +Math.exp(x)); // return (a power of 2)-1 System.out.println("expm1 of a is: " +Math.expm1(x)); } } Test it NowOutput: Maximum number of x and y is: 28.0 Square root of y is: 2.0 Power of x and y is: 614656.0 Logarithm of x is: 3.332204510175204 Logarithm of y is: 1.3862943611198906 log10 of x is: 1.4471580313422192 log10 of y is: 0.6020599913279624 log1p of x is: 3.367295829986474 exp of a is: 1.446257064291475E12 expm1 of a is: 1.446257064290475E12 Example 2public class JavaMathExample2 { public static void main(String[] args) { double a = 30; // converting values to radian double b = Math.toRadians(a); // return the trigonometric sine of a System.out.println("Sine value of a is: " +Math.sin(a)); // return the trigonometric cosine value of a System.out.println("Cosine value of a is: " +Math.cos(a)); // return the trigonometric tangent value of a System.out.println("Tangent value of a is: " +Math.tan(a)); // return the trigonometric arc sine of a System.out.println("Sine value of a is: " +Math.asin(a)); // return the trigonometric arc cosine value of a System.out.println("Cosine value of a is: " +Math.acos(a)); // return the trigonometric arc tangent value of a System.out.println("Tangent value of a is: " +Math.atan(a)); // return the hyperbolic sine of a System.out.println("Sine value of a is: " +Math.sinh(a)); // return the hyperbolic cosine value of a System.out.println("Cosine value of a is: " +Math.cosh(a)); // return the hyperbolic tangent value of a System.out.println("Tangent value of a is: " +Math.tanh(a)); } } Output: Sine value of a is: -0.9880316240928618 Cosine value of a is: 0.15425144988758405 Tangent value of a is: -6.405331196646276 Sine value of a is: NaN Cosine value of a is: NaN Tangent value of a is: 1.5374753309166493 Sine value of a is: 5.343237290762231E12 Cosine value of a is: 5.343237290762231E12 Tangent value of a is: 1.0 Java Math MethodsThe java.lang.Math class contains various methods for performing basic numeric operations such as the logarithm, cube root, and trigonometric functions etc. The various java math methods are as follows: Basic Math methods
Logarithmic Math Methods
Trigonometric Math Methods
Hyperbolic Math Methods
Angular Math Methods
Next TopicWrapper class in Java |