Java Math Class

The java.lang.Math class is a utility class that provides various mathematical functions and constants, such as basic arithmetic operations, rounding functions, and the values of mathematical constants like PI and E. It is a final class, which means that it cannot be subclassed. You can use the Math class to perform mathematical operations in your Java programs, such as calculating the circumference of a circle or finding the exponent of a number. You can learn more about the Math class and the functions it provides in the Java documentation.

Java Math

The java.lang.Math class is a utility class that provides various mathematical functions and constants. It is a final class, which means that it cannot be subclassed.

Here are some examples of the functions provided by the Math class:

  • Basic arithmetic operations: addExact, subtractExact, multiplyExact, incrementExact, decrementExact, negateExact, abs, max, min, toIntExact, etc.
  • Rounding functions: round, ceil, floor, rint, nextUp, nextDown, etc.
  • Trigonometric functions: sin, cos, tan, asin, acos, atan, atan2, etc.
  • Exponential and logarithmic functions: exp, log, log10, log1p, expm1, etc.
  • Power and square root functions: pow, sqrt, cbrt, hypot, etc.

The Math class also provides the following constants:

  • PI: The mathematical constant pi.
  • E: The mathematical constant e.

Here is an example of how you can use the Math class in a Java program:

public class Main {
  public static void main(String[] args) {
    // calculate the circumference of a circle with radius 5
    double radius = 5;
    double circumference = 2 * Math.PI * radius;  // circumference is 31.41592653589793
    
    // find the exponent of 2 to the power of 3
    double exponent = 3;
    double result = Math.pow(2, exponent);  // result is 8.0
  }
}

Java Math Class Methods With Example

The java.lang.Math class includes several methods for performing basic mathematical operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Here are some examples:

  • abs(x): Returns the absolute value of x. For example, abs(-1) returns 1.
  • max(x, y): Returns the greater of two int values. For example, max(1, 2) returns 2.
  • min(x, y): Returns the smaller of two int values. For example, min(1, 2) returns 1.
  • sqrt(x): Returns the square root of x. For example, sqrt(9) returns 3.0.
  • pow(x, y): Returns the value of x raised to the power of y. For example, pow(2, 3) returns 8.0.
  • log(x): Returns the natural logarithm (base e) of x. For example, log(Math.E) returns 1.0.
  • sin(x), cos(x), tan(x): Returns the sine, cosine, and tangent of x, respectively. x is assumed to be in radians.
  • toDegrees(x), toRadians(x): Converts x from degrees to radians and from radians to degrees, respectively.

Basic Math Operations

MethodDescription
abs(x)Returns the absolute value of x.
max(x, y)Returns the greater of two int values.
min(x, y)Returns the smaller of two int values.
pow(x, y)Returns the value of x raised to the power of y.
sqrt(x)Returns the square root of x.

Trigonometry

MethodDescription
sin(x)Returns the sine of x.
cos(x)Returns the cosine of x.
tan(x)Returns the tangent of x.

Exponentials and Logarithms

MethodDescription
exp(x)Returns the value of e raised to the power of x.
log(x)Returns the natural logarithm (base e) of x.
log10(x)Returns the logarithm (base 10) of x.

Rounding

MethodDescription
ceil(x)Returns the smallest (closest to negative infinity) double value that is greater than or equal to x and is equal to a mathematical integer.
floor(x)Returns the largest (closest to positive infinity) double value that is less than or equal to x and is equal to a mathematical integer.
rint(x)Returns the closest int to x.
round(x)Returns the closest long to x.

Conversion

MethodDescription
toDegrees(x)Converts x from radians to degrees.
toRadians(x)Converts x from degrees to radians.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *