Java

Math Class In Java Programming Language

Numerous helpful methods are provided by the Java Math class, which may be used to carry out a variety of mathematical computations. It uses trigonometric, logarithmic, and fundamental arithmetic techniques. We will go through all of the math class techniques with examples in this tutorial.

After the following examples, there is a list at the bottom of this page with all the methods of the Java math class.

Example 1: max(), min() and pow() Methods

public class JavaExample
{
  public static void main(String[] args)
  {
    double a = 5;
    double b = 3;
    System.out.println("Values of a and b are: "+a+", "+b);
    // Maximum of two numbers
    System.out.println("Maximum Number between a and b: " +Math.max(a, b));
    // Minimum of two numbers
    System.out.println("Minimum Number between a and b: " +Math.min(a, b));
    // a to the power b: 5^3 = 125
    System.out.println("a to be power b: " + Math.pow(a, b));
  }
}

Output:

Math Class In Java Programming Language

Example 2: addExact(), multiplyExact() and subtractExact() Methods

public class JavaExample
{
  public static void main(String[] args)
  {
    int a = 10;
    int b = 2;
    System.out.println("Values of a and b are: "+a+", "+b);
    System.out.println("a+b: " +Math.addExact(a, b));
    System.out.println("a*b: " +Math.multiplyExact(a, b));
    System.out.println("a-b: " +Math.subtractExact(a, b));
  }
}

Output:

Math Class In Java Programming Language

Example 3: incrementExact(), decrementExact(), negateExact() and toIntExact() Methods

public class JavaExample
{
  public static void main(String[] args)
  {
    int a = 10;
    long b = 20;
    System.out.println("Values of a and b: "+a+", "+b);
    System.out.println("a+1: " +Math.incrementExact(a));
    System.out.println("a-1: " +Math.decrementExact(a));
    //changes the sign
    System.out.println("-a: " +Math.negateExact(a));
    //converts long to int
    System.out.println("b as int: " +Math.toIntExact(b));
  }
}

Output:

Math Class In Java Programming Language

Example 4: sqrt(), cbrt() and random() Methods

public class JavaExample
{
  public static void main(String[] args)
  {
    int a = 25;
    int b = 27;
    System.out.println("Value of a and b: "+a+", "+b);
    System.out.println("Square root of a: "+Math.sqrt(a));
    System.out.println("Cube root of b: "+Math.cbrt(b));
    System.out.println("Random number between 1 and 100: "+
            Math.random()*100);
  }
}

Output:

Math Class In Java Programming Language

Example 5: ceil(), floor() and round() methods

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

Output:

Math Class In Java Programming Language

Example 6: Java Math Logarithmic Methods

public class JavaExample
{
  public static void main(String[] args)
  {
    double a = 10;
    System.out.println("Values of a: "+a);
    // natural logarithm of a
    System.out.println("Natural Logarithm of a is: " + Math.log(a));
    // base 10 logarithm of a
    System.out.println("Base 10 Logarithm of a is: " + Math.log10(a));
    // e to the power of a: e^a where e approx. value 2.71828
    System.out.println("exp of a is: " +Math.exp(a));
  }
}

Output:

log, log10, exp methods in Math class

Example 7: Java Math Class Trigonometric Methods

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 45;
    System.out.println("Value of variable num is: "+num);

    System.out.println("Radian Value of num: "+Math.toRadians(num));
    System.out.println("Sine value of num: "+Math.sin(num));
    System.out.println("Cosine value of num: " +Math.cos(num));
    System.out.println("Tangent value of num: " +Math.tan(num));
    System.out.println("Arc sine value of num: " +Math.asin(num));
    System.out.println("Arc cosine value of num: " +Math.acos(num));
    System.out.println("Arc tangent of num: " +Math.atan(num));
    System.out.println("Hyperbolic Sine of num: " +Math.sinh(num));
    System.out.println("Hyperbolic Cosine of num: " +Math.cosh(num));
    System.out.println("Hyperbolic Tangent of num: " +Math.tanh(num));
  }
}

Output:

Math Class In Java Programming Language

Java Math Class Methods List

1. Basic Arithmetic Methods

Method Description
Math.random() Returns a random positive double value between 0.0 and 1.0
Math.max() Returns the greater value between the two values passed as arguments.
Math.min() Returns the smaller value between the two values passed as arguments.
Math.sqrt() Returns square root of the given value. The result is a positive rounded double value.
Math.pow() Returns the value of first argument raised to the power of second argument.
Math.abs() Returns the absolute value of the passed argument.
Math.round() Returns the closest number to the passed argument.
Math.cbrt() Returns cube root of the given value.
Math.signum() Returns the sign of the given number.
Math.ceil() Returns the closest int value to the passed number.
Math.nextUp() Returns the floating point value nearest to the passed value in the direction of positive affinity.
Math.nextDown() Returns the floating point value nearest to the passed value in the direction of negative affinity.
Math.nextAfter() Returns the floating point value nearest to the first argument in the direction of second argument
Math.copySign() Returns the first floating point value passed as first argument with the sign of second argument.
Math.floor() Returns the nearest int value to the passed value.
Math.floorDiv() Returns largest value that is less than or equal to the quotient generated by dividing first argument with second argument.
Math.rint() Returns closest int value to the passed float value.
Math.hypot() Returns sqrt(x2 +y2) without intermediate overflow or underflow.
Math.ulp() Returns the size of an ulp of the argument. Accuracy of the floating-point Math methods is measured in terms of ulps, units in the last place.
Math.addExact() Returns the sum of its arguments.
Math.subtractExact() Returns the difference of its arguments.
Math.multiplyExact() Returns the product of its arguments.
Math.incrementExact() Returns the argument incremented by one.
Math.decrementExact() Returns the argument decremented by one.
Math.negateExact() Returns the negation of the passed argument.
Math.toIntExact() Returns the value of the long argument; throwing an exception if the value overflows an int.
Math.getExponent() Returns exponent of the argument.
Math.IEEEremainder() Returns the remainder after dividing the first argument by second argument as per the IEEE 754 standard.

2. Logarithmic Methods

Method Description
Math.log() Returns natural logarithmic value of the passed double argument.
Math.log10() Returns base 10 logarithmic value of the passed double argument.
Math.log1p() Returns natural logarithmic value of the sum of passed argument and 1.
Math.exp() Returns e to the power given value. Here, e is a Euler’s number and its approx. value is 2.71828
Math.expm1() Returns e to the power given value minus 1.

3. Trigonometric Methods

Method Description
Math.sin() Returns Sine value of the given double value.
Math.cos() Returns Cosine value of the given double value.
Math.tan() Returns Tangent value of the given double value.
Math.asin() Returns Arc sine value of the given double value.
Math.acos() Returns Arc Cosine value of the given double value.
Math.atan() Returns Arc Tangent value of the given double value.
Math.sinh() Returns Hyperbolic Sine value of the given double value.
Math.cosh() Returns Hyperbolic Cosine value of the given double value.
Math.tanh() Returns Hyperbolic Tangent value of the given double value.
Math.toDegrees() Converts given radian value to degrees.
Math.toRadians() Converts given value in degrees to radians.

Related Articles

Leave a Reply

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

Back to top button