0% found this document useful (0 votes)
10 views8 pages

Topic 6_Math Class in Java

The Java Math class provides a variety of mathematical functions and constants for calculations, such as square roots, exponentials, and trigonometric functions. It is part of the java.lang package, allowing direct access without an import statement. The document includes examples of using the Math class methods and describes various mathematical operations available in Java.

Uploaded by

joyikehi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views8 pages

Topic 6_Math Class in Java

The Java Math class provides a variety of mathematical functions and constants for calculations, such as square roots, exponentials, and trigonometric functions. It is part of the java.lang package, allowing direct access without an import statement. The document includes examples of using the Math class methods and describes various mathematical operations available in Java.

Uploaded by

joyikehi7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

JAVA MATH CLASS

Java is a powerful programming language that offers a wide range of


functionalities and features. One important aspect of Java is its Math class,
which provides various methods for performing mathematical calculations.

What is Math Class in Java?


The Math class provides a wide range of mathematical functions and constants
that are useful for various calculations. Some of the commonly used methods
include finding square roots, calculating exponential values, rounding
numbers, and generating random numbers.
The Math class is part of the java.lang package, it is automatically accessible
in every Java program. This means that you can directly use the methods and
constants of the Math class without importing it explicitly. For example, you
can use the Math.PI constant to access the value of pi or the Math.sqrt()
method to calculate square roots.

Method 1: Importing Math Class Without Using Import Statement


In Java, you can use the Math class without importing it explicitly because it
belongs to java.lang package. This allows you to access the methods and
constants of the Math class directly using the class name.
Here's an example:

Example 1: Accessing the PI Variable


The Math class provides a constant variable called PI, which represents the
mathematical constant pi (approximately 3.141592653589793). You can
directly access this variable without importing the Math class. Here's an
example:
System.out.println("The value of PI: " + Math.PI);

Output:
The value of PI: 3.141592653589793
Example 2: Finding the Maximum Number
The Math class provides a method called max() that returns the greater of two
values. You can use this method without importing the Math class. Here's an
example:
int num1 = 40;
int num2 = 87;
System.out.println("The maximum number: " + Math.max(num1, num2));

Output:
The maximum number: 87

Example 3: Calculating Square Roots


The Math class provides a method called sqrt() that returns the square root of
a number. You can use this method directly without importing the Math class.
Here's an example:

int number = 100;


System.out.println("The square root of 100 is: " + Math.sqrt(number));

Output:
The square root of 100 is: 10.0

Example 4: Calculating Power


The Math class provides a method called pow() that returns the power xy of a
number. You can use this method directly without importing the Math class.
Here's an example:

int number = 2;
System.out.println("The power of 3 is: " + Math.pow(number, 3));

Output:
The square root of 100 is: 10.0

Method 2: Importing Math Class Using Import Statement


While you can use the Math class without importing it explicitly, you can also
import it using the import statement. This allows you to use the methods and
constants of the Math class without specifying the class name.
Here's an example:

import static java.lang.Math.*;

public class Main {


public static void main(String[] args) {
System.out.println("The value of PI: " + PI);
System.out.println("The maximum number: " + max(40, 87));
System.out.println("The square root of 100 is: " + sqrt(100));
}
}

Output:
The value of PI: 3.141592653589793
The maximum number: 87
The square root of 100 is: 10.0

Using the Math Class in Java Programs

1. Finding the Area of a Circle


To find the area of a circle, you can use the Math.pow() method to calculate
the square of the radius and then multiply it by the value of pi.

import static java.lang.Math.*;


public class Main {
public static void main(String[] args) {
int radius = 5;
double area = PI * pow(radius, 2);
System.out.println("The area of the circle is: " + area);
}
}

Output:
The area of the circle is: 78.53981633974483

2. Calculating Exponential Values


The Math class provides a method called exp() that returns the exponential
value of a number.

import static java.lang.Math.*;


public class Main {

public static void main(String[] args) {

double base = 2;
double exponent = 3;
double result = exp(exponent * log(base));

System.out.println("The exponential value is: " + result);

}
}

Output:
The exponential value is: 8.0

3: Performing Trigonometric Calculations


The Math class provides various methods for performing trigonometric
calculations, such as sin(), cos(), and tan().

import static java.lang.Math.*;

public class Main {

public static void main(String[] args) {

doube angle = PI / 4;
double sine = sin(angle);
double cosine = cos(angle);
double tangent = tan(angle);

System.out.println("Sine: " + sine);


System.out.println("Cosine: " + cosine);
System.out.println("Tangent: " + tangent);
}
}

Output:
Sine: 0.7071067811865475
Cosine: 0.7071067811865476
Tangent: 0.9999999999999999

Basic Math methods

Method Description

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

Method Description

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

Method Description

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

Method Description

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.

You might also like