Java Reference
In-Depth Information
The
“
a%b
„
modulo operator returns the remainder of
a
divided by
b
.For
example,
5%3
returns a value of 2. The modulo operator also works with floating-point values.
If either operand is floating-point, the remainder is a floating-point type.
Note that the
Math
class (see below) includes the method
Math.IEEEremainder (double a, double b)
which computes the remainder of
a/b
for two double type values according to
the specific rules of the IEEE 754 standard (see the
Math
class in the Java 2 API
Specifications).
Java does not include an exponentiation operator, such as
x**b
for
x
to the
power of
b
. Instead, you must use the
Math
class method
Math.pow (double a, double b)
which computes
a
to the power of
b
and returns a
double
type.
2.13.2 Math functions
Beyond the basic arithmetic operations, a number of mathematical functions and
constants are available in the core Java language via the
Math
class. We discuss
exactly what a class is in the next chapter so for now just accept that the function
name must be preceded by
“
Math.
„
.
Math
is a class that comes as part of the core language. It offers many useful
features:
Constants:
Math.PI
Math.E
Trigonometric functions (radian units):
double x
=
.5;
double y
=
Math.sin (x * Math.PI);
x =
Math.asin (y);
Absolute values:
int i = Math.abs (j);
Random number generators:
double x = Math.random (); // In the range:
// 0.0 <= x < 1.0
Other:
double y = Math.sqrt (x);
double x = Math.exp (y);
Search WWH ::
Custom Search