public class FpUtils
extends java.lang.Object
FpUtils
contains static utility methods for
manipulating and inspecting float
and
double
floating-point numbers. These methods include
functionality recommended or required by the IEEE 754
floating-point standard.限定符和类型 | 方法和说明 |
---|---|
static double |
copySign(double magnitude,
double sign)
Returns the first floating-point argument with the sign of the
second floating-point argument.
|
static float |
copySign(float magnitude,
float sign)
Returns the first floating-point argument with the sign of the
second floating-point argument.
|
static int |
getExponent(double d)
Returns unbiased exponent of a
double . |
static int |
getExponent(float f)
Returns unbiased exponent of a
float . |
static int |
ilogb(double d)
Returns unbiased exponent of a
double ; for
subnormal values, the number is treated as if it were
normalized. |
static int |
ilogb(float f)
Returns unbiased exponent of a
float ; for
subnormal values, the number is treated as if it were
normalized. |
static boolean |
isFinite(double d)
Returns
true if the argument is a finite
floating-point value; returns false otherwise (for
NaN and infinity arguments). |
static boolean |
isFinite(float f)
Returns
true if the argument is a finite
floating-point value; returns false otherwise (for
NaN and infinity arguments). |
static boolean |
isInfinite(double d)
Returns
true if the specified number is infinitely
large in magnitude, false otherwise. |
static boolean |
isInfinite(float f)
Returns
true if the specified number is infinitely
large in magnitude, false otherwise. |
static boolean |
isNaN(double d)
Returns
true if the specified number is a
Not-a-Number (NaN) value, false otherwise. |
static boolean |
isNaN(float f)
Returns
true if the specified number is a
Not-a-Number (NaN) value, false otherwise. |
static boolean |
isUnordered(double arg1,
double arg2)
Returns
true if the unordered relation holds
between the two arguments. |
static boolean |
isUnordered(float arg1,
float arg2)
Returns
true if the unordered relation holds
between the two arguments. |
static double |
nextAfter(double start,
double direction)
Returns the floating-point number adjacent to the first
argument in the direction of the second argument.
|
static float |
nextAfter(float start,
double direction)
Returns the floating-point number adjacent to the first
argument in the direction of the second argument.
|
static double |
nextDown(double d)
Returns the floating-point value adjacent to
d in
the direction of negative infinity. |
static double |
nextDown(float f)
Returns the floating-point value adjacent to
f in
the direction of negative infinity. |
static double |
nextUp(double d)
Returns the floating-point value adjacent to
d in
the direction of positive infinity. |
static float |
nextUp(float f)
Returns the floating-point value adjacent to
f in
the direction of positive infinity. |
static double |
rawCopySign(double magnitude,
double sign)
Returns the first floating-point argument with the sign of the
second floating-point argument.
|
static float |
rawCopySign(float magnitude,
float sign)
Returns the first floating-point argument with the sign of the
second floating-point argument.
|
static double |
scalb(double d,
int scale_factor)
Return
d ×
2scale_factor rounded as if performed
by a single correctly rounded floating-point multiply to a
member of the double value set. |
static float |
scalb(float f,
int scale_factor)
Return
f ×
2scale_factor rounded as if performed
by a single correctly rounded floating-point multiply to a
member of the float value set. |
static double |
signum(double d)
Returns the signum function of the argument; zero if the argument
is zero, 1.0 if the argument is greater than zero, -1.0 if the
argument is less than zero.
|
static float |
signum(float f)
Returns the signum function of the argument; zero if the argument
is zero, 1.0f if the argument is greater than zero, -1.0f if the
argument is less than zero.
|
static double |
ulp(double d)
Returns the size of an ulp of the argument.
|
static float |
ulp(float f)
Returns the size of an ulp of the argument.
|
public static int getExponent(double d)
double
.public static int getExponent(float f)
float
.public static double rawCopySign(double magnitude, double sign)
copySign
method, this method
does not require NaN sign
arguments to be treated
as positive values; implementations are permitted to treat some
NaN arguments as positive and other NaN arguments as negative
to allow greater performance.magnitude
- the parameter providing the magnitude of the resultsign
- the parameter providing the sign of the resultmagnitude
and the sign of sign
.public static float rawCopySign(float magnitude, float sign)
copySign
method, this method
does not require NaN sign
arguments to be treated
as positive values; implementations are permitted to treat some
NaN arguments as positive and other NaN arguments as negative
to allow greater performance.magnitude
- the parameter providing the magnitude of the resultsign
- the parameter providing the sign of the resultmagnitude
and the sign of sign
.public static boolean isFinite(double d)
true
if the argument is a finite
floating-point value; returns false
otherwise (for
NaN and infinity arguments).d
- the double
value to be testedtrue
if the argument is a finite
floating-point value, false
otherwise.public static boolean isFinite(float f)
true
if the argument is a finite
floating-point value; returns false
otherwise (for
NaN and infinity arguments).f
- the float
value to be testedtrue
if the argument is a finite
floating-point value, false
otherwise.public static boolean isInfinite(double d)
true
if the specified number is infinitely
large in magnitude, false
otherwise.
Note that this method is equivalent to the Double.isInfinite
method; the
functionality is included in this class for convenience.
d
- the value to be tested.true
if the value of the argument is positive
infinity or negative infinity; false
otherwise.public static boolean isInfinite(float f)
true
if the specified number is infinitely
large in magnitude, false
otherwise.
Note that this method is equivalent to the Float.isInfinite
method; the
functionality is included in this class for convenience.
f
- the value to be tested.true
if the argument is positive infinity or
negative infinity; false
otherwise.public static boolean isNaN(double d)
true
if the specified number is a
Not-a-Number (NaN) value, false
otherwise.
Note that this method is equivalent to the Double.isNaN
method; the functionality is
included in this class for convenience.
d
- the value to be tested.true
if the value of the argument is NaN;
false
otherwise.public static boolean isNaN(float f)
true
if the specified number is a
Not-a-Number (NaN) value, false
otherwise.
Note that this method is equivalent to the Float.isNaN
method; the functionality is
included in this class for convenience.
f
- the value to be tested.true
if the argument is NaN;
false
otherwise.public static boolean isUnordered(double arg1, double arg2)
true
if the unordered relation holds
between the two arguments. When two floating-point values are
unordered, one value is neither less than, equal to, nor
greater than the other. For the unordered relation to be true,
at least one argument must be a NaN
.arg1
- the first argumentarg2
- the second argumenttrue
if at least one argument is a NaN,
false
otherwise.public static boolean isUnordered(float arg1, float arg2)
true
if the unordered relation holds
between the two arguments. When two floating-point values are
unordered, one value is neither less than, equal to, nor
greater than the other. For the unordered relation to be true,
at least one argument must be a NaN
.arg1
- the first argumentarg2
- the second argumenttrue
if at least one argument is a NaN,
false
otherwise.public static int ilogb(double d)
double
; for
subnormal values, the number is treated as if it were
normalized. That is for all finite, non-zero, positive numbers
x, scalb(x, -ilogb(x))
is
always in the range [1, 2).
Special cases:
d
- floating-point number whose exponent is to be extractedpublic static int ilogb(float f)
float
; for
subnormal values, the number is treated as if it were
normalized. That is for all finite, non-zero, positive numbers
x, scalb(x, -ilogb(x))
is
always in the range [1, 2).
Special cases:
f
- floating-point number whose exponent is to be extractedpublic static double scalb(double d, int scale_factor)
d
×
2scale_factor
rounded as if performed
by a single correctly rounded floating-point multiply to a
member of the double value set. See §4.2.3
of the Java
Language Specification for a discussion of floating-point
value sets. If the exponent of the result is between the
double
's minimum exponent and maximum exponent,
the answer is calculated exactly. If the exponent of the
result would be larger than doubles
's maximum
exponent, an infinity is returned. Note that if the result is
subnormal, precision may be lost; that is, when scalb(x,
n)
is subnormal, scalb(scalb(x, n), -n)
may
not equal x. When the result is non-NaN, the result has
the same sign as d
.
Special cases:
d
- number to be scaled by a power of two.scale_factor
- power of 2 used to scale d
d *
2scale_factor
public static float scalb(float f, int scale_factor)
f
×
2scale_factor
rounded as if performed
by a single correctly rounded floating-point multiply to a
member of the float value set. See §4.2.3
of the Java
Language Specification for a discussion of floating-point
value set. If the exponent of the result is between the
float
's minimum exponent and maximum exponent, the
answer is calculated exactly. If the exponent of the result
would be larger than float
's maximum exponent, an
infinity is returned. Note that if the result is subnormal,
precision may be lost; that is, when scalb(x, n)
is subnormal, scalb(scalb(x, n), -n)
may not equal
x. When the result is non-NaN, the result has the same
sign as f
.
Special cases:
f
- number to be scaled by a power of two.scale_factor
- power of 2 used to scale f
f *
2scale_factor
public static double nextAfter(double start, double direction)
Special cases:
direction
is returned unchanged (as implied by the requirement of
returning the second argument if the arguments compare as
equal).
start
is
±Double.MIN_VALUE
and direction
has a value such that the result should have a smaller
magnitude, then a zero with the same sign as start
is returned.
start
is infinite and
direction
has a value such that the result should
have a smaller magnitude, Double.MAX_VALUE
with the
same sign as start
is returned.
start
is equal to ±
Double.MAX_VALUE
and direction
has a
value such that the result should have a larger magnitude, an
infinity with same sign as start
is returned.
start
- starting floating-point valuedirection
- value indicating which of
start
's neighbors or start
should
be returnedstart
in the
direction of direction
.public static float nextAfter(float start, double direction)
Special cases:
float
zero with the same sign as direction
is returned
(as implied by the requirement of returning the second argument
if the arguments compare as equal).
start
is
±Float.MIN_VALUE
and direction
has a value such that the result should have a smaller
magnitude, then a zero with the same sign as start
is returned.
start
is infinite and
direction
has a value such that the result should
have a smaller magnitude, Float.MAX_VALUE
with the
same sign as start
is returned.
start
is equal to ±
Float.MAX_VALUE
and direction
has a
value such that the result should have a larger magnitude, an
infinity with same sign as start
is returned.
start
- starting floating-point valuedirection
- value indicating which of
start
's neighbors or start
should
be returnedstart
in the
direction of direction
.public static double nextUp(double d)
d
in
the direction of positive infinity. This method is
semantically equivalent to nextAfter(d,
Double.POSITIVE_INFINITY)
; however, a nextUp
implementation may run faster than its equivalent
nextAfter
call.
Special Cases:
Double.MIN_VALUE
d
- starting floating-point valuepublic static float nextUp(float f)
f
in
the direction of positive infinity. This method is
semantically equivalent to nextAfter(f,
Double.POSITIVE_INFINITY)
; however, a nextUp
implementation may run faster than its equivalent
nextAfter
call.
Special Cases:
Float.MIN_VALUE
f
- starting floating-point valuepublic static double nextDown(double d)
d
in
the direction of negative infinity. This method is
semantically equivalent to nextAfter(d,
Double.NEGATIVE_INFINITY)
; however, a
nextDown
implementation may run faster than its
equivalent nextAfter
call.
Special Cases:
-Double.MIN_VALUE
d
- starting floating-point valuepublic static double nextDown(float f)
f
in
the direction of negative infinity. This method is
semantically equivalent to nextAfter(f,
Float.NEGATIVE_INFINITY)
; however, a
nextDown
implementation may run faster than its
equivalent nextAfter
call.
Special Cases:
-Float.MIN_VALUE
f
- starting floating-point valuepublic static double copySign(double magnitude, double sign)
sign
argument is always treated as if it were
positive.magnitude
- the parameter providing the magnitude of the resultsign
- the parameter providing the sign of the resultmagnitude
and the sign of sign
.public static float copySign(float magnitude, float sign)
sign
argument is always treated as if it were
positive.magnitude
- the parameter providing the magnitude of the resultsign
- the parameter providing the sign of the resultmagnitude
and the sign of sign
.public static double ulp(double d)
double
value is the positive distance between this
floating-point value and the double
value next
larger in magnitude. Note that for non-NaN x,
ulp(-x) == ulp(x)
.
Special Cases:
Double.MIN_VALUE
.
Double.MAX_VALUE
, then
the result is equal to 2971.
d
- the floating-point value whose ulp is to be returnedpublic static float ulp(float f)
float
value is the positive distance between this
floating-point value and the float
value next
larger in magnitude. Note that for non-NaN x,
ulp(-x) == ulp(x)
.
Special Cases:
Float.MIN_VALUE
.
Float.MAX_VALUE
, then
the result is equal to 2104.
f
- the floating-point value whose ulp is to be returnedpublic static double signum(double d)
Special Cases:
d
- the floating-point value whose signum is to be returnedpublic static float signum(float f)
Special Cases:
f
- the floating-point value whose signum is to be returned