Java Reference
In-Depth Information
The
Math
Class
The class
Math
provides a number of standard mathematical methods. The class
Math
is provided automatically and requires no
import
statement. Some of the methods in
the class
Math
are described in Display 5.6. A more complete list of methods is given in
Appendix 4. All of these methods are static, which means that you normally use the
class name
Math
in place of a calling object.
Math
methods
Display 5.6
Some Methods in the Class
Math
(part 1 of 2)
The
Math
class is in the
java.lang
package, so it requires no
import
statement.
public static double
pow(
double
base,
double
exponent)
Returns
base
to the power
exponent
.
EXAMPLE
Math.pow(2.0,3.0)
returns
8.0
.
public static double
abs(
double
argument)
public static float
abs(
float
argument)
public static long
abs(
long
argument)
public static int
abs(
int
argument)
Returns the absolute value of the
argument
. (The method name
abs
is overloaded to produce four
similar methods.)
EXAMPLE
Math.abs(
−
6)
and
Math.abs(6)
both return
6
.
Math.abs(
−
5.5)
and
Math.abs(5.5)
both return
5.5
.
public static double
min(
double
n1,
double
n2)
public static float
min(
float
n1,
float
n2)
public static long
min(
long
n1,
long
n2)
public static int
min(
int
n1,
int
n2)
Returns the minimum of the arguments
n1
and
n2
. (The method name
min
is overloaded to produce
four similar methods.)
EXAMPLE
Math.min(3, 2)
returns
2
.
public static double
max(
double
n1,
double
n2)
public static float
max(
float
n1,
float
n2)
public static long
max(
long
n1,
long
n2)
public static int
max(
int
n1,
int
n2)
Returns the maximum of the arguments
n1
and
n2
. (The method name
max
is overloaded to produce
four similar methods.)
EXAMPLE
Math.max(3, 2)
returns
3
.
(continued)