Math Class

The Math class is a built-in class in many programming languages, such as Java, Python, and C#. It provides a wide range of mathematical functions that can be used to perform various calculations. Here are some common methods of the Math class:

  1. abs(x): Returns the absolute value of x.

  2. sqrt(x): Returns the square root of x.

  3. pow(x, y): Returns x raised to the power of y.

  4. log(x): Returns the natural logarithm (base e) of x.

  5. log10(x): Returns the base 10 logarithm of x.

  6. exp(x): Returns e raised to the power of x.

  7. sin(x): Returns the sine of x (where x is in radians).

  8. cos(x): Returns the cosine of x (where x is in radians).

  9. tan(x): Returns the tangent of x (where x is in radians).

  10. asin(x): Returns the arcsine of x (where x is in radians).

  11. acos(x): Returns the arccosine of x (where x is in radians).

  12. atan(x): Returns the arctangent of x (where x is in radians).

  13. floor(x): Returns the largest integer that is less than or equal to x.

  14. ceil(x): Returns the smallest integer that is greater than or equal to x.

  15. round(x): Rounds x to the nearest integer.

  16. max(x, y): Returns the larger of x and y.

  17. min(x, y): Returns the smaller of x and y.

  18. random(): Returns a random double value between 0.0 and 1.0.

  19. toRadians(x): Converts an angle in degrees to radians.

  20. toDegrees(x): Converts an angle in radians to degrees.

Example of how to use the Math class in Java

refer to:‮aeditfigi‬.com
public class MathExample {
    public static void main(String[] args) {
        // Calculate the absolute value of a number
        int absoluteValue = Math.abs(-5);
        System.out.println("Absolute value of -5 is: " + absoluteValue);

        // Calculate the square root of a number
        double squareRoot = Math.sqrt(25);
        System.out.println("Square root of 25 is: " + squareRoot);

        // Calculate the power of a number
        double power = Math.pow(2, 3);
        System.out.println("2 to the power of 3 is: " + power);

        // Calculate the maximum value between two numbers
        int max = Math.max(10, 20);
        System.out.println("The maximum value between 10 and 20 is: " + max);

        // Calculate the minimum value between two numbers
        int min = Math.min(10, 20);
        System.out.println("The minimum value between 10 and 20 is: " + min);

        // Generate a random number between 0 and 1
        double random = Math.random();
        System.out.println("A random number between 0 and 1 is: " + random);
    }
}

In this example, we use the Math class to perform various mathematical operations.