Java Reference
In-Depth Information
Method
Function
Argument Type
Result Type
round(arg)
Calculates the nearest integer
to the argument value
float
or
double
Of type
int
for
a
float
argument, of
type
long
for a
double
argument
rint(arg)
double
double
Calculates the nearest integer
to the argument value
IEEEremainder
(arg1,arg2)
Calculates the remainder
when
arg1
is divided by
arg2
Both of type
double
Of type
double
The
IEEEremainder()
method produces the remainder from
arg1
after dividing
arg2
into
arg1
the
integral number of times that is closest to the exact value of
arg1/arg2
. This is somewhat different
from the remainder operator. The operation
arg1
%
arg2
produces the remainder after dividing
arg2
into
arg1
the integral number of times that does not exceed the absolute value of
arg1
. In some
situations this can result in markedly different results. For example, executing the expression
9.0
%
5.0
results in
4.0
, whereas the expression
Math.IEEEremainder(9.0,5.0)
results in
-1.0
. You can
pick one approach to calculating the remainder or the other, to suit your requirements.
Where more than one type of argument is noted in the table, there are actually several methods, one for
each type of argument, but all have the same name. We will see how this is possible in Java when we
look at implementing class methods in Chapter 5.
The mathematical functions available in the class
Math
are:
Method
Function
Argument Type
Result Type
sqrt(arg)
Calculates the square root of
the argument
double
double
pow
(arg1,arg2)
Calculates the first argument
raised to the power of the
second argument
arg1arg2
Both
double
double
exp(arg)
Calculates e raised to the
power of the argument e
arg
double
double
log(arg)
double
double
Calculates the natural
logarithm (base e) of the
argument
random()
Returns a pseudo-random
number greater than or equal
to 0.0 and less than 1.0
None
double
The
toRadians()
method in the class
Math
will convert a
double
argument that is an angular
measurement in degrees to radians. There is a complementary method,
toDegrees()
, to convert in
the opposite direction. The
Math
class also defines
double
values for e and
π
, which you can access as
Math.E
and
Math.PI
respectively.