Package com.sun.ts.lib.util
Class FpUtils
java.lang.Object
com.sun.ts.lib.util.FpUtils
The class
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.- Author:
- Joseph D. Darcy
-
Method Summary
Modifier and TypeMethodDescriptionstatic doublecopySign(double magnitude, double sign) Returns the first floating-point argument with the sign of the second floating-point argument.static floatcopySign(float magnitude, float sign) Returns the first floating-point argument with the sign of the second floating-point argument.static intgetExponent(double d) Returns unbiased exponent of adouble.static intgetExponent(float f) Returns unbiased exponent of afloat.static intilogb(double d) Returns unbiased exponent of adouble; for subnormal values, the number is treated as if it were normalized.static intilogb(float f) Returns unbiased exponent of afloat; for subnormal values, the number is treated as if it were normalized.static booleanisFinite(double d) Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).static booleanisFinite(float f) Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).static booleanisInfinite(double d) Returnstrueif the specified number is infinitely large in magnitude,falseotherwise.static booleanisInfinite(float f) Returnstrueif the specified number is infinitely large in magnitude,falseotherwise.static booleanisNaN(double d) Returnstrueif the specified number is a Not-a-Number (NaN) value,falseotherwise.static booleanisNaN(float f) Returnstrueif the specified number is a Not-a-Number (NaN) value,falseotherwise.static booleanisUnordered(double arg1, double arg2) Returnstrueif the unordered relation holds between the two arguments.static booleanisUnordered(float arg1, float arg2) Returnstrueif the unordered relation holds between the two arguments.static doublenextAfter(double start, double direction) Returns the floating-point number adjacent to the first argument in the direction of the second argument.static floatnextAfter(float start, double direction) Returns the floating-point number adjacent to the first argument in the direction of the second argument.static doublenextDown(double d) Returns the floating-point value adjacent todin the direction of negative infinity.static doublenextDown(float f) Returns the floating-point value adjacent tofin the direction of negative infinity.static doublenextUp(double d) Returns the floating-point value adjacent todin the direction of positive infinity.static floatnextUp(float f) Returns the floating-point value adjacent tofin the direction of positive infinity.static doublerawCopySign(double magnitude, double sign) Returns the first floating-point argument with the sign of the second floating-point argument.static floatrawCopySign(float magnitude, float sign) Returns the first floating-point argument with the sign of the second floating-point argument.static doublescalb(double d, int scale_factor) Returnd× 2scale_factorrounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.static floatscalb(float f, int scale_factor) Returnf× 2scale_factorrounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.static doublesignum(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 floatsignum(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 doubleulp(double d) Returns the size of an ulp of the argument.static floatulp(float f) Returns the size of an ulp of the argument.
-
Method Details
-
getExponent
public static int getExponent(double d) Returns unbiased exponent of adouble. -
getExponent
public static int getExponent(float f) Returns unbiased exponent of afloat. -
rawCopySign
public static double rawCopySign(double magnitude, double sign) Returns the first floating-point argument with the sign of the second floating-point argument. Note that unlike thecopySignmethod, this method does not require NaNsignarguments 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.- Parameters:
magnitude- the parameter providing the magnitude of the resultsign- the parameter providing the sign of the result- Returns:
- a value with the magnitude of
magnitudeand the sign ofsign.
-
rawCopySign
public static float rawCopySign(float magnitude, float sign) Returns the first floating-point argument with the sign of the second floating-point argument. Note that unlike thecopySignmethod, this method does not require NaNsignarguments 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.- Parameters:
magnitude- the parameter providing the magnitude of the resultsign- the parameter providing the sign of the result- Returns:
- a value with the magnitude of
magnitudeand the sign ofsign.
-
isFinite
public static boolean isFinite(double d) Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).- Parameters:
d- thedoublevalue to be tested- Returns:
trueif the argument is a finite floating-point value,falseotherwise.
-
isFinite
public static boolean isFinite(float f) Returnstrueif the argument is a finite floating-point value; returnsfalseotherwise (for NaN and infinity arguments).- Parameters:
f- thefloatvalue to be tested- Returns:
trueif the argument is a finite floating-point value,falseotherwise.
-
isInfinite
public static boolean isInfinite(double d) Returnstrueif the specified number is infinitely large in magnitude,falseotherwise. Note that this method is equivalent to theDouble.isInfinitemethod; the functionality is included in this class for convenience.- Parameters:
d- the value to be tested.- Returns:
trueif the value of the argument is positive infinity or negative infinity;falseotherwise.
-
isInfinite
public static boolean isInfinite(float f) Returnstrueif the specified number is infinitely large in magnitude,falseotherwise. Note that this method is equivalent to theFloat.isInfinitemethod; the functionality is included in this class for convenience.- Parameters:
f- the value to be tested.- Returns:
trueif the argument is positive infinity or negative infinity;falseotherwise.
-
isNaN
public static boolean isNaN(double d) Returnstrueif the specified number is a Not-a-Number (NaN) value,falseotherwise. Note that this method is equivalent to theDouble.isNaNmethod; the functionality is included in this class for convenience.- Parameters:
d- the value to be tested.- Returns:
trueif the value of the argument is NaN;falseotherwise.
-
isNaN
public static boolean isNaN(float f) Returnstrueif the specified number is a Not-a-Number (NaN) value,falseotherwise. Note that this method is equivalent to theFloat.isNaNmethod; the functionality is included in this class for convenience.- Parameters:
f- the value to be tested.- Returns:
trueif the argument is NaN;falseotherwise.
-
isUnordered
public static boolean isUnordered(double arg1, double arg2) Returnstrueif 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 aNaN.- Parameters:
arg1- the first argumentarg2- the second argument- Returns:
trueif at least one argument is a NaN,falseotherwise.
-
isUnordered
public static boolean isUnordered(float arg1, float arg2) Returnstrueif 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 aNaN.- Parameters:
arg1- the first argumentarg2- the second argument- Returns:
trueif at least one argument is a NaN,falseotherwise.
-
ilogb
public static int ilogb(double d) Returns unbiased exponent of adouble; 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:- If the argument is NaN, then the result is 230.
- If the argument is infinite, then the result is 228.
- If the argument is zero, then the result is -(228).
- Parameters:
d- floating-point number whose exponent is to be extracted- Returns:
- unbiased exponent of the argument.
-
ilogb
public static int ilogb(float f) Returns unbiased exponent of afloat; 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:- If the argument is NaN, then the result is 230.
- If the argument is infinite, then the result is 228.
- If the argument is zero, then the result is -(228).
- Parameters:
f- floating-point number whose exponent is to be extracted- Returns:
- unbiased exponent of the argument.
-
scalb
public static double scalb(double d, int scale_factor) Returnd× 2scale_factorrounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. See section 4.2.3 of The Java™ Language Specification for a discussion of floating-point value sets. If the exponent of the result is between thedouble's minimum exponent and maximum exponent, the answer is calculated exactly. If the exponent of the result would be larger thandoubles's maximum exponent, an infinity is returned. Note that if the result is subnormal, precision may be lost; that is, whenscalb(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 asd. Special cases:- If the first argument is NaN, NaN is returned.
- If the first argument is infinite, then an infinity of the same sign is returned.
- If the first argument is zero, then a zero of the same sign is returned.
- Parameters:
d- number to be scaled by a power of two.scale_factor- power of 2 used to scaled- Returns:
d *2scale_factor
-
scalb
public static float scalb(float f, int scale_factor) Returnf× 2scale_factorrounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. See section 4.2.3 of The Java™ Language Specification for a discussion of floating-point value sets. If the exponent of the result is between thefloat's minimum exponent and maximum exponent, the answer is calculated exactly. If the exponent of the result would be larger thanfloat's maximum exponent, an infinity is returned. Note that if the result is subnormal, precision may be lost; that is, whenscalb(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 asf. Special cases:- If the first argument is NaN, NaN is returned.
- If the first argument is infinite, then an infinity of the same sign is returned.
- If the first argument is zero, then a zero of the same sign is returned.
- Parameters:
f- number to be scaled by a power of two.scale_factor- power of 2 used to scalef- Returns:
f *2scale_factor
-
nextAfter
public static double nextAfter(double start, double direction) Returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal the second argument is returned. Special cases:- If either argument is a NaN, then NaN is returned.
- If both arguments are signed zeros,
directionis returned unchanged (as implied by the requirement of returning the second argument if the arguments compare as equal). - If
startis ±Double.MIN_VALUEanddirectionhas a value such that the result should have a smaller magnitude, then a zero with the same sign asstartis returned. - If
startis infinite anddirectionhas a value such that the result should have a smaller magnitude,Double.MAX_VALUEwith the same sign asstartis returned. - If
startis equal to ±Double.MAX_VALUEanddirectionhas a value such that the result should have a larger magnitude, an infinity with same sign asstartis returned.
- Parameters:
start- starting floating-point valuedirection- value indicating which ofstart's neighbors orstartshould be returned- Returns:
- The floating-point number adjacent to
startin the direction ofdirection.
-
nextAfter
public static float nextAfter(float start, double direction) Returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal, the second argument is returned. Special cases:- If either argument is a NaN, then NaN is returned.
- If both arguments are signed zeros, a
floatzero with the same sign asdirectionis returned (as implied by the requirement of returning the second argument if the arguments compare as equal). - If
startis ±Float.MIN_VALUEanddirectionhas a value such that the result should have a smaller magnitude, then a zero with the same sign asstartis returned. - If
startis infinite anddirectionhas a value such that the result should have a smaller magnitude,Float.MAX_VALUEwith the same sign asstartis returned. - If
startis equal to ±Float.MAX_VALUEanddirectionhas a value such that the result should have a larger magnitude, an infinity with same sign asstartis returned.
- Parameters:
start- starting floating-point valuedirection- value indicating which ofstart's neighbors orstartshould be returned- Returns:
- The floating-point number adjacent to
startin the direction ofdirection.
-
nextUp
public static double nextUp(double d) Returns the floating-point value adjacent todin the direction of positive infinity. This method is semantically equivalent tonextAfter(d, Double.POSITIVE_INFINITY); however, anextUpimplementation may run faster than its equivalentnextAftercall. Special Cases:- If the argument is NaN, the result is NaN.
- If the argument is positive infinity, the result is positive infinity.
- If the argument is zero, the result is
Double.MIN_VALUE
- Parameters:
d- starting floating-point value- Returns:
- The adjacent floating-point value closer to positive infinity.
-
nextUp
public static float nextUp(float f) Returns the floating-point value adjacent tofin the direction of positive infinity. This method is semantically equivalent tonextAfter(f, Double.POSITIVE_INFINITY); however, anextUpimplementation may run faster than its equivalentnextAftercall. Special Cases:- If the argument is NaN, the result is NaN.
- If the argument is positive infinity, the result is positive infinity.
- If the argument is zero, the result is
Float.MIN_VALUE
- Parameters:
f- starting floating-point value- Returns:
- The adjacent floating-point value closer to positive infinity.
-
nextDown
public static double nextDown(double d) Returns the floating-point value adjacent todin the direction of negative infinity. This method is semantically equivalent tonextAfter(d, Double.NEGATIVE_INFINITY); however, anextDownimplementation may run faster than its equivalentnextAftercall. Special Cases:- If the argument is NaN, the result is NaN.
- If the argument is negative infinity, the result is negative infinity.
- If the argument is zero, the result is
-Double.MIN_VALUE
- Parameters:
d- starting floating-point value- Returns:
- The adjacent floating-point value closer to negative infinity.
-
nextDown
public static double nextDown(float f) Returns the floating-point value adjacent tofin the direction of negative infinity. This method is semantically equivalent tonextAfter(f, Float.NEGATIVE_INFINITY); however, anextDownimplementation may run faster than its equivalentnextAftercall. Special Cases:- If the argument is NaN, the result is NaN.
- If the argument is negative infinity, the result is negative infinity.
- If the argument is zero, the result is
-Float.MIN_VALUE
- Parameters:
f- starting floating-point value- Returns:
- The adjacent floating-point value closer to negative infinity.
-
copySign
public static double copySign(double magnitude, double sign) Returns the first floating-point argument with the sign of the second floating-point argument. For this method, a NaNsignargument is always treated as if it were positive.- Parameters:
magnitude- the parameter providing the magnitude of the resultsign- the parameter providing the sign of the result- Returns:
- a value with the magnitude of
magnitudeand the sign ofsign. - Since:
- 1.5
-
copySign
public static float copySign(float magnitude, float sign) Returns the first floating-point argument with the sign of the second floating-point argument. For this method, a NaNsignargument is always treated as if it were positive.- Parameters:
magnitude- the parameter providing the magnitude of the resultsign- the parameter providing the sign of the result- Returns:
- a value with the magnitude of
magnitudeand the sign ofsign.
-
ulp
public static double ulp(double d) Returns the size of an ulp of the argument. An ulp of adoublevalue is the positive distance between this floating-point value and thedoublevalue next larger in magnitude. Note that for non-NaN x,ulp(-x) == ulp(x). Special Cases:- If the argument is NaN, then the result is NaN.
- If the argument is positive or negative infinity, then the result is positive infinity.
- If the argument is positive or negative zero, then the result is
Double.MIN_VALUE. - If the argument is ±
Double.MAX_VALUE, then the result is equal to 2971.
- Parameters:
d- the floating-point value whose ulp is to be returned- Returns:
- the size of an ulp of the argument
- Since:
- 1.5
-
ulp
public static float ulp(float f) Returns the size of an ulp of the argument. An ulp of afloatvalue is the positive distance between this floating-point value and thefloatvalue next larger in magnitude. Note that for non-NaN x,ulp(-x) == ulp(x). Special Cases:- If the argument is NaN, then the result is NaN.
- If the argument is positive or negative infinity, then the result is positive infinity.
- If the argument is positive or negative zero, then the result is
Float.MIN_VALUE. - If the argument is ±
Float.MAX_VALUE, then the result is equal to 2104.
- Parameters:
f- the floating-point value whose ulp is to be returned- Returns:
- the size of an ulp of the argument
- Since:
- 1.5
-
signum
public 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. Special Cases:- If the argument is NaN, then the result is NaN.
- If the argument is positive zero or negative zero, then the result is the same as the argument.
- Parameters:
d- the floating-point value whose signum is to be returned- Returns:
- the signum function of the argument
- Since:
- 1.5
-
signum
public 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. Special Cases:- If the argument is NaN, then the result is NaN.
- If the argument is positive zero or negative zero, then the result is the same as the argument.
- Parameters:
f- the floating-point value whose signum is to be returned- Returns:
- the signum function of the argument
- Since:
- 1.5
-