In Java, the `java.lang.Math` class provides a set of predefined mathematical functions that cover a wide range of operations. These functions are static methods, meaning you call them directly using the class name (`Math`) without creating an instance of the class. Here are some commonly used mathematical functions available in the `Math` class:
### Basic Mathematical Functions:
1. **Square Root (`sqrt`)**:
- Computes the square root of a number.
double number = 25;
double squareRoot = Math.sqrt(number); // squareRoot is 5.0
```
2. **Exponentiation (`exp`)**:
- Computes the base raised to the power of the exponent (`e^x`).
double exponent = 2.0;
double result = Math.exp(exponent); // result is approximately 7.389
```
3. **Power (`pow`)**:
- Computes the base raised to the power of the exponent (`base^exponent`).
double base = 2.0;
double exponent = 3.0;
double result = Math.pow(base, exponent); // result is 8.0 (2^3)
```
4. **Absolute Value (`abs`)**:
- Returns the absolute value of a number.
int number = -10;
int absValue = Math.abs(number); // absValue is 10
```
5. **Round (`round`)**:
- Rounds a floating-point number to the nearest integer.
double number = 3.7;
long rounded = Math.round(number); // rounded is 4 (returns a long)
```
6. **Minimum (`min`) and Maximum (`max`)**:
- Returns the smaller or larger of two values.
int a = 10, b = 5;
int minValue = Math.min(a, b); // minValue is 5
int maxValue = Math.max(a, b); // maxValue is 10
```
### Trigonometric Functions:
Java also provides trigonometric functions, which operate on angles (in radians):
1. **Sine (`sin`)**, **Cosine (`cos`)**, **Tangent (`tan`)**:
- Computes the sine, cosine, and tangent of an angle, respectively.
double angleInRadians = Math.PI / 2; // 90 degrees
double sineValue = Math.sin(angleInRadians); // sineValue is 1.0
double cosineValue = Math.cos(angleInRadians); // cosineValue is approximately 0.0
double tangentValue = Math.tan(angleInRadians); // tangentValue is Infinity (undefined at 90 degrees)
```
2. **Arcsine (`asin`)**, **Arccosine (`acos`)**, **Arctangent (`atan`)**:
- Computes the inverse trigonometric functions.
double value = 1.0;
double arcSine = Math.asin(value); // arcSine is approximately 1.5708 radians (90 degrees)
double arcCosine = Math.acos(value); // arcCosine is 0.0 radians (0 degrees)
double arcTangent = Math.atan(value); // arcTangent is approximately 0.7854 radians (45 degrees)
```
### Constants:
The `Math` class also defines some useful constants:
- **π (`PI`)**: The value of π (pi), approximately 3.14159.
- **e (`E`)**: The base of natural logarithms, approximately 2.71828.
double piValue = Math.PI; // piValue is approximately 3.14159
double eValue = Math.E; // eValue is approximately 2.71828
### Using `StrictMath` for Strictly IEEE 754-Compliant Math Operations:
Java also provides the `java.lang.StrictMath` class, which implements strictly IEEE 754-compliant versions of the same mathematical functions as `Math`. The differences are generally negligible unless you require extremely precise mathematical calculations.
These mathematical functions in Java provide powerful tools for performing calculations across various domains, from basic arithmetic to advanced scientific and engineering applications.