Next: Expressions that involve different Up: Unit 04 Previous: Precision in measures

## Predefined static methods for mathematical operations

To calculate mathematical functions on values of a numeric type, Java defines some classes containing static methods that can be used to calculate such functions. For example, the predefined class Math contains several such methods, e.g., to calculate the square root (sqrt()), the absolute value (abs()), trigonometric functions (sin(), cos(), tan()), etc.

The following table is taken from the official documentation of the Java APIs:

 Method Summary `static double` `abs(double a)`           Returns the absolute value of a `double` value. `static float` `abs(float a)`           Returns the absolute value of a `float` value. `static int` `abs(int a)`           Returns the absolute value of an `int` value. `static long` `abs(long a)`           Returns the absolute value of a `long` value. `static double` `acos(double a)`           Returns the arc cosine of an angle, in the range of 0.0 through pi. `static double` `asin(double a)`           Returns the arc sine of an angle, in the range of -pi/2 through pi/2. `static double` `atan(double a)`           Returns the arc tangent of an angle, in the range of -pi/2 through pi/2. `static double` ```atan2(double a, double b)```           Converts rectangular coordinates (`b`, `a`) to polar (r, theta). `static double` `ceil(double a)`           Returns the smallest (closest to negative infinity) `double` value that is not less than the argument and is equal to a mathematical integer. `static double` `cos(double a)`           Returns the trigonometric cosine of an angle. `static double` `exp(double a)`           Returns the exponential number e (i.e., 2.718...) raised to the power of a `double` value. `static double` `floor(double a)`           Returns the largest (closest to positive infinity) `double` value that is not greater than the argument and is equal to a mathematical integer. `static double` ```IEEEremainder(double f1, double f2)```           Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. `static double` `log(double a)`           Returns the natural logarithm (base e) of a `double` value. `static double` ```max(double a, double b)```           Returns the greater of two `double` values. `static float` ```max(float a, float b)```           Returns the greater of two `float` values. `static int` ```max(int a, int b)```           Returns the greater of two `int` values. `static long` ```max(long a, long b)```           Returns the greater of two `long` values. `static double` ```min(double a, double b)```           Returns the smaller of two `double` values. `static float` ```min(float a, float b)```           Returns the smaller of two `float` values. `static int` ```min(int a, int b)```           Returns the smaller of two `int` values. `static long` ```min(long a, long b)```           Returns the smaller of two `long` values. `static double` ```pow(double a, double b)```           Returns of value of the first argument raised to the power of the second argument. `static double` `random()`           Returns a `double` value with a positive sign, greater than or equal to `0.0` and less than `1.0`. `static double` `rint(double a)`           Returns the `double` value that is closest in value to `a` and is equal to a mathematical integer. `static long` `round(double a)`           Returns the closest `long` to the argument. `static int` `round(float a)`           Returns the closest `int` to the argument. `static double` `sin(double a)`           Returns the trigonometric sine of an angle. `static double` `sqrt(double a)`           Returns the correctly rounded positive square root of a `double` value. `static double` `tan(double a)`           Returns the trigonometric tangent of an angle. `static double` `toDegrees(double angrad)`           Converts an angle measured in radians to the equivalent angle measured in degrees. `static double` `toRadians(double angdeg)`           Converts an angle measured in degrees to the equivalent angle measured in radians.

Example:

```int j = -2;
System.out.println(Math.abs(j));    // prints 2
```

Next: Expressions that involve different Up: Unit 04 Previous: Precision in measures