Java Math

Introduction

The Math class in Java provides static methods for various mathematical operations, including arithmetic, trigonometry, logarithms, and more. It is part of the java.lang package and cannot be instantiated.

Table of Contents

  1. What is Math?
  2. Common Methods
  3. Examples of Math Methods
  4. Conclusion

1. What is Math?

Math is a utility class that offers methods for mathematical functions, constants, and calculations.

2. Common Methods

  • abs(double a): Returns the absolute value of a number.
  • max(int a, int b): Returns the greater of two values.
  • min(int a, int b): Returns the smaller of two values.
  • pow(double a, double b): Returns the value of a raised to the power of b.
  • sqrt(double a): Returns the square root of a number.
  • cbrt(double a): Returns the cube root of a number.
  • ceil(double a): Returns the smallest integer greater than or equal to a.
  • floor(double a): Returns the largest integer less than or equal to a.
  • round(double a): Returns the closest long to the argument.
  • exp(double a): Returns e raised to the power of a.
  • log(double a): Returns the natural logarithm of a number.
  • log10(double a): Returns the base 10 logarithm of a number.
  • sin(double a), cos(double a), tan(double a): Trigonometric functions.
  • asin(double a), acos(double a), atan(double a): Inverse trigonometric functions.
  • sinh(double a), cosh(double a), tanh(double a): Hyperbolic functions.
  • random(): Returns a double value greater than or equal to 0.0 and less than 1.0.
  • toRadians(double angdeg): Converts an angle from degrees to radians.
  • toDegrees(double angrad): Converts an angle from radians to degrees.

3. Examples of Math Methods

Example 1: Absolute Value

public class MathAbsExample {
    public static void main(String[] args) {
        int a = -10;
        System.out.println("Absolute value of " + a + " is: " + Math.abs(a));
    }
}

Output:

Absolute value of -10 is: 10

Example 2: Maximum and Minimum

public class MathMaxMinExample {
    public static void main(String[] args) {
        int a = 5, b = 10;
        System.out.println("Max of " + a + " and " + b + ": " + Math.max(a, b));
        System.out.println("Min of " + a + " and " + b + ": " + Math.min(a, b));
    }
}

Output:

Max of 5 and 10: 10
Min of 5 and 10: 5

Example 3: Power and Square Root

public class MathPowSqrtExample {
    public static void main(String[] args) {
        double base = 2.0, exponent = 3.0;
        System.out.println(base + " raised to " + exponent + " is: " + Math.pow(base, exponent));
        System.out.println("Square root of " + base + " is: " + Math.sqrt(base));
    }
}

Output:

2.0 raised to 3.0 is: 8.0
Square root of 2.0 is: 1.4142135623730951

Example 4: Trigonometric Functions

public class MathTrigExample {
    public static void main(String[] args) {
        double angle = Math.PI / 2;
        System.out.println("Sin of PI/2: " + Math.sin(angle));
        System.out.println("Cos of PI/2: " + Math.cos(angle));
        System.out.println("Tan of PI/2: " + Math.tan(angle));
    }
}

Output:

Sin of PI/2: 1.0
Cos of PI/2: 6.123233995736766E-17
Tan of PI/2: 1.633123935319537E16

Example 5: Rounding Functions

public class MathRoundingExample {
    public static void main(String[] args) {
        double number = 2.7;
        System.out.println("Ceil of " + number + ": " + Math.ceil(number));
        System.out.println("Floor of " + number + ": " + Math.floor(number));
        System.out.println("Round of " + number + ": " + Math.round(number));
    }
}

Output:

Ceil of 2.7: 3.0
Floor of 2.7: 2.0
Round of 2.7: 3

Example 6: Random Number Generation

public class MathRandomExample {
    public static void main(String[] args) {
        double randomValue = Math.random();
        System.out.println("Random value: " + randomValue);
    }
}

Output:

Random value: 0.42756136439384396

4. Java Math Class Methods

The table below contains all the commonly used methods of the Java Math class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
abs() Returns the absolute value of a given number.
absExact() Returns the absolute value of a given exact number.
addExact() Returns the sum of its arguments, throwing an exception if the result overflows an int or long.
asin() Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.
atan() Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
atan2() Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
cbrt() Returns the cube root of a double value.
ceil() Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
ceilDiv() Returns the division of its arguments, rounded towards positive infinity.
ceilDivExact() Returns the exact division of its arguments, throwing an exception if the result overflows an int or long.
ceilMod() Returns the remainder of division as an integer, rounded towards positive infinity.
clamp() Returns a value constrained to a specified range.
copySign() Returns the first floating-point argument with the sign of the second floating-point argument.
cos() Returns the trigonometric cosine of an angle.
cosh() Returns the hyperbolic cosine of a double value.
floorDiv() Returns the largest (closest to negative infinity) integer value that is less than or equal to the algebraic quotient.
floorMod() Returns the floor modulus of the int or long arguments.
fma() Returns the exact product of the first two arguments summed with the third argument.
log() Returns the natural logarithm (base e) of a double value.
log1p() Returns the natural logarithm of the sum of the argument and 1.
max() Returns the greater of two values.
min() Returns the smaller of two values.
multiplyFull() Returns the full precision product of the arguments.
negateExact() Returns the negation of the argument, throwing an exception if the result overflows.
nextDown() Returns the floating-point value adjacent to the argument in the direction of negative infinity.
pow() Returns the value of the first argument raised to the power of the second argument.
random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
round() Returns the closest long or int to the argument.
sin() Returns the trigonometric sine of an angle.
sqrt() Returns the correctly rounded positive square root of a double value.

5. Conclusion

The Math class in Java provides a wide range of methods for mathematical operations. It is a valuable utility for performing calculations, conversions, and more in Java programming.

Comments