Math Class :
- Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.
- 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.
public class JavaMathExample1
{
public static void main(String[] args)
{
double x = 35;
double y = 8;
// 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));
}
}
Output :
Maximum number of x and y is: 35.0
Square root of y is: 2.8284271247461903
Power of x and y is: 2.251875390625E12
Logarithm of x is: 3.5553480614894135
Logarithm of y is: 2.0794415416798357
log10 of x is: 1.5440680443502757
log10 of y is: 0.9030899869919435
log1p of x is: 3.58351893845611
exp of a is: 1.5860134523134308E15
expm1 of a is: 1.5860134523134298E15
Math.max(x, y) : Returns the maximum of x and y.
- Example output : Maximum number of x and y is: 35.0
Math.sqrt(y) : Returns the square root of y.
- Example output : Square root of y is: 2.8284271247461903 (approximately)
Math.pow(x, y) : Returns x raised to the power of y.
- Example output : Power of x and y is: 3.0517578125E15 (approximately)
Math.log(x) : Returns the natural logarithm (base e) of x.
- Example output : Logarithm of x is: 3.5553480614894135
Math.log10(x) : Returns the base 10 logarithm of x.
- Example output : log10 of x is: 1.5440680443502757
Math.log1p(x) : Returns the natural logarithm of (x + 1).
- Example output : log1p of x is: 3.58351893845611
Math.exp(x) : Returns e raised to the power of x.
- Example output : exp of a is: 1.5860134504631328E15 (approximately)
Math.expm1(x) : Returns e raised to the power of x, minus 1.
- Example output : expm1 of a is: 1.5860134504631328E15 (approximately, very close to Math.exp(x) due to large x value)
public class JavaMathExample2
{
public static void main(String[] args)
{
double a = 60;
// 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.3048106211022167
Cosine value of a is: -0.9524129804151563
Tangent value of a is: 0.320040389379563
Sine value of a is: NaN
Cosine value of a is: NaN
Tangent value of a is: 1.554131203080956
Sine value of a is: 5.710036949078421E25
Cosine value of a is: 5.710036949078421E25
Tangent value of a is: 1.0
- Math.toRadians(a) : Converts angle a from degrees to radians. This is necessary because trigonometric and hyperbolic functions in Java operate on radians.
- Math.sin(b) : Returns the sine of angle b (which is a converted to radians).
- Math.cos(b) : Returns the cosine of angle b.
- Math.tan(b) : Returns the tangent of angle b.
- Math.asin(Math.sin(b)) : Returns the arc sine (inverse sine) of the sine of angle b. Since Math.sin(b) returns a value between -1 and 1, Math.asin returns an angle in radians between -π/2 and π/2.
- Math.acos(Math.cos(b)) : Returns the arc cosine (inverse cosine) of the cosine of angle b. Similarly, Math.acos returns an angle in radians between 0 and π.
- Math.atan(Math.tan(b)) : Returns the arc tangent (inverse tangent) of the tangent of angle b. Math.atan returns an angle in radians between -π/2 and π/2.
- Math.sinh(b) : Returns the hyperbolic sine of angle b.
- Math.cosh(b) : Returns the hyperbolic cosine of angle b.
- Math.tanh(b) : Returns the hyperbolic tangent of angle b.
Basic Math Methods :
- Math.abs() : It will return the Absolute value of the given value.
- Math.max() : It returns the Largest of two values.
- Math.min() : It is used to return the Smallest of two values.
- Math.round() : It is used to round of the decimal numbers to the nearest value.
- Math.sqrt() : It is used to return the square root of a?number.
- Math.cbrt() : It is used to return the cube root of a?number.
- Math.pow() : It returns the value of first argument raised to the power to second argument.
- Math.signum() : It is used to find the sign of a given value.
- Math.ceil() : It is used to find the smallest integer value that is greater than or equal to the argument or mathematical integer.
- Math.copySign() : It is used to find the Absolute value of first argument along with sign specified in second argument.
- Math.nextAfter() : It is used to return the floating-point number adjacent to the first argument in the direction of the second argument.
- Math.nextUp() : It returns the floating-point value adjacent to?d?in the direction of positive infinity.
- Math.nextDown() : It returns the floating-point value adjacent to?d?in the direction of negative infinity.
- Math.floor() : It is used to find the?largest integer value which is less than or equal to the argument and is equal to the mathematical integer of a double value.
- Math.floorDiv() : It is used to find the?largest integer value that is less than or equal to the algebraic quotient.
- Math.random() : It returns a?double?value with a positive sign, greater than or equal to?0.0?and less than?1.0.
- Math.rint() : It returns the double value that is closest to the given argument and equal to mathematical integer.
- Math.hypot() : It returns sqrt(x2?+y2) without intermediate overflow or underflow.
- Math.ulp() : It returns the size of an ulp of the argument.
- Math.getExponent() : It is used to return the unbiased exponent used in the representation of a?value.
- Math.IEEEremainder() : It is used to calculate the remainder operation on two arguments as prescribed by the IEEE 754 standard and returns value.
- Math.addExact() : It is used to return the sum of its arguments, throwing an exception if the result overflows an?int or long.
- Math.subtractExact() : It returns the difference of the arguments, throwing an exception if the result overflows an?int.
- Math.multiplyExact() : It is used to return the product of the arguments, throwing an exception if the result overflows an?int or long.
- Math.incrementExact() : It returns the argument incremented by one, throwing an exception if the result overflows an?int.
- Math.decrementExact() : It is used to return the argument decremented by one, throwing an exception if the result overflows an?int or long.
- Math.negateExact() : It is used to return the negation of the argument, throwing an exception if the result overflows an?int or long.
- Math.toIntExact() : It returns the value of the?long?argument, throwing an exception if the value overflows an?int.
Logarithmic Math Methods :
- Math.log() : It returns the natural logarithm of a?double?value.
- Math.log10() :It is used to return the base 10 logarithm of a?double?value.
- Math.log1p() : It returns the natural logarithm of the sum of the argument and 1.
- Math.exp() : It returns E raised to the power of a?double?value, where E is Euler's number and it is approximately equal to 2.71828.
- Math.expm1() : It is used to calculate the power of E and subtract one from it.
Trigonometric Math Methods :
- Math.sin() : It is used to return the trigonometric Sine value of a Given double value.
- Math.cos() : It is used to return the trigonometric Cosine value of a Given double value.
- Math.tan() : It is used to return the trigonometric Tangent value of a Given double value.
- Math.asin() : It is used to return the trigonometric Arc Sine value of a Given double value.
- Math.acos() : It is used to return the trigonometric Arc Cosine value of a Given double value.
- Math.atan() : It is used to return the trigonometric Arc Tangent value of a Given double value.
Hyperbolic Math Methods :
- Math.sinh() : It is used to return the trigonometric Hyperbolic Cosine value of a Given double value.
- Math.cosh() : It is used to return the trigonometric Hyperbolic Sine value of a Given double value.
- Math.tanh() : It is used to return the trigonometric Hyperbolic Tangent value of a Given double value.
Angular Math Methods :
- Math.toDegrees : It is used to convert the specified Radians angle to equivalent angle measured in Degrees.
- Math.toRadians : It is used to convert the specified Degrees angle to equivalent angle measured in Radians.