Home >Java >javaTutorial >How to use math in java
The Math class in Java provides mathematical calculation functions, including trigonometric functions, exponential functions and utility methods, which can be called directly without creating an instance. Commonly used methods include trigonometric functions (sin, cos, tan), exponential functions (pow, exp, log) and other methods (abs, ceil, floor, round, min, max, random), used to calculate trigonometric function values, exponential value, generate random numbers, get absolute values, round numbers and get maximum and minimum values.
Usage of Math class in Java
Math class is a class used for mathematical calculations in Java. It provides commonly used mathematical functions such as trigonometric functions, exponential functions and some other useful mathematical methods.
Usage Methods
The Math class is a static class, which means that its methods can be used directly without creating an instance. To use the Math class, simply call its methods directly where needed.
Commonly used methods
Specific usage examples
Calculate trigonometric function value:
<code class="java">double angle = Math.toRadians(90); double sine = Math.sin(angle);</code>
Calculate exponential value:
<code class="java">double base = 2.0; double exponent = 3.0; double result = Math.pow(base, exponent);</code>
Generate random number:
<code class="java">double random = Math.random();</code>
Get the absolute value of the number:
<code class="java">int number = -10; int absolute = Math.abs(number);</code>
Round the number:
<code class="java">double number = 3.14; int rounded = Math.round(number);</code>
Get the maximum or minimum value of the number Value:
<code class="java">int a = 10; int b = 20; int max = Math.max(a, b); int min = Math.min(a, b);</code>
The above is the detailed content of How to use math in java. For more information, please follow other related articles on the PHP Chinese website!