提交 fe7ceb20 编写于 作者: D darcy

7002594: Math.max and Math.min should use floatToRawIntBits() to check for -0.0

Reviewed-by: mduigou, lancea, alanb
上级 89389743
/* /*
* Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -973,7 +973,8 @@ public final class Double extends Number implements Comparable<Double> { ...@@ -973,7 +973,8 @@ public final class Double extends Number implements Comparable<Double> {
if (d1 > d2) if (d1 > d2)
return 1; // Neither val is NaN, thisVal is larger return 1; // Neither val is NaN, thisVal is larger
long thisBits = Double.doubleToLongBits(d1); // Cannot use doubleToRawLongBits because of possibility of NaNs.
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2); long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // Values are equal return (thisBits == anotherBits ? 0 : // Values are equal
......
/* /*
* Copyright (c) 1994, 2009, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -872,12 +872,13 @@ public final class Float extends Number implements Comparable<Float> { ...@@ -872,12 +872,13 @@ public final class Float extends Number implements Comparable<Float> {
* @since 1.4 * @since 1.4
*/ */
public static int compare(float f1, float f2) { public static int compare(float f1, float f2) {
if (f1 < f2) if (f1 < f2)
return -1; // Neither val is NaN, thisVal is smaller return -1; // Neither val is NaN, thisVal is smaller
if (f1 > f2) if (f1 > f2)
return 1; // Neither val is NaN, thisVal is larger return 1; // Neither val is NaN, thisVal is larger
int thisBits = Float.floatToIntBits(f1); // Cannot use floatToRawIntBits because of possibility of NaNs.
int thisBits = Float.floatToIntBits(f1);
int anotherBits = Float.floatToIntBits(f2); int anotherBits = Float.floatToIntBits(f2);
return (thisBits == anotherBits ? 0 : // Values are equal return (thisBits == anotherBits ? 0 : // Values are equal
......
/* /*
* Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -801,8 +801,9 @@ public final class StrictMath { ...@@ -801,8 +801,9 @@ public final class StrictMath {
return (a >= b) ? a : b; return (a >= b) ? a : b;
} }
private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f); // Use raw bit-wise conversions on guaranteed non-NaN arguments.
private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d); private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
/** /**
* Returns the greater of two {@code float} values. That is, * Returns the greater of two {@code float} values. That is,
...@@ -819,9 +820,12 @@ public final class StrictMath { ...@@ -819,9 +820,12 @@ public final class StrictMath {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static float max(float a, float b) { public static float max(float a, float b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0f) && (b == 0.0f) return a; // a is NaN
&& (Float.floatToIntBits(a) == negativeZeroFloatBits)) { if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a >= b) ? a : b; return (a >= b) ? a : b;
...@@ -842,9 +846,12 @@ public final class StrictMath { ...@@ -842,9 +846,12 @@ public final class StrictMath {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static double max(double a, double b) { public static double max(double a, double b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0d) && (b == 0.0d) return a; // a is NaN
&& (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) { if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a >= b) ? a : b; return (a >= b) ? a : b;
...@@ -893,9 +900,12 @@ public final class StrictMath { ...@@ -893,9 +900,12 @@ public final class StrictMath {
* @return the smaller of {@code a} and {@code b.} * @return the smaller of {@code a} and {@code b.}
*/ */
public static float min(float a, float b) { public static float min(float a, float b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0f) && (b == 0.0f) return a; // a is NaN
&& (Float.floatToIntBits(b) == negativeZeroFloatBits)) { if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a <= b) ? a : b; return (a <= b) ? a : b;
...@@ -916,9 +926,12 @@ public final class StrictMath { ...@@ -916,9 +926,12 @@ public final class StrictMath {
* @return the smaller of {@code a} and {@code b}. * @return the smaller of {@code a} and {@code b}.
*/ */
public static double min(double a, double b) { public static double min(double a, double b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0d) && (b == 0.0d) return a; // a is NaN
&& (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) { if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a <= b) ? a : b; return (a <= b) ? a : b;
......
/* /*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2010 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -29,9 +29,9 @@ import sun.misc.FloatConsts; ...@@ -29,9 +29,9 @@ import sun.misc.FloatConsts;
import sun.misc.DoubleConsts; import sun.misc.DoubleConsts;
/** /**
* The class <code>FpUtils</code> contains static utility methods for * The class {@code FpUtils} contains static utility methods for
* manipulating and inspecting <code>float</code> and * manipulating and inspecting {@code float} and
* <code>double</code> floating-point numbers. These methods include * {@code double} floating-point numbers. These methods include
* functionality recommended or required by the IEEE 754 * functionality recommended or required by the IEEE 754
* floating-point standard. * floating-point standard.
* *
...@@ -136,7 +136,7 @@ public class FpUtils { ...@@ -136,7 +136,7 @@ public class FpUtils {
// tests for exception cases. // tests for exception cases.
/** /**
* Returns unbiased exponent of a <code>double</code>. * Returns unbiased exponent of a {@code double}.
*/ */
public static int getExponent(double d){ public static int getExponent(double d){
/* /*
...@@ -149,7 +149,7 @@ public class FpUtils { ...@@ -149,7 +149,7 @@ public class FpUtils {
} }
/** /**
* Returns unbiased exponent of a <code>float</code>. * Returns unbiased exponent of a {@code float}.
*/ */
public static int getExponent(float f){ public static int getExponent(float f){
/* /*
...@@ -185,15 +185,15 @@ public class FpUtils { ...@@ -185,15 +185,15 @@ public class FpUtils {
* Returns the first floating-point argument with the sign of the * Returns the first floating-point argument with the sign of the
* second floating-point argument. Note that unlike the {@link * second floating-point argument. Note that unlike the {@link
* FpUtils#copySign(double, double) copySign} method, this method * FpUtils#copySign(double, double) copySign} method, this method
* does not require NaN <code>sign</code> arguments to be treated * does not require NaN {@code sign} arguments to be treated
* as positive values; implementations are permitted to treat some * as positive values; implementations are permitted to treat some
* NaN arguments as positive and other NaN arguments as negative * NaN arguments as positive and other NaN arguments as negative
* to allow greater performance. * to allow greater performance.
* *
* @param magnitude the parameter providing the magnitude of the result * @param magnitude the parameter providing the magnitude of the result
* @param sign the parameter providing the sign of the result * @param sign the parameter providing the sign of the result
* @return a value with the magnitude of <code>magnitude</code> * @return a value with the magnitude of {@code magnitude}
* and the sign of <code>sign</code>. * and the sign of {@code sign}.
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static double rawCopySign(double magnitude, double sign) { public static double rawCopySign(double magnitude, double sign) {
...@@ -208,15 +208,15 @@ public class FpUtils { ...@@ -208,15 +208,15 @@ public class FpUtils {
* Returns the first floating-point argument with the sign of the * Returns the first floating-point argument with the sign of the
* second floating-point argument. Note that unlike the {@link * second floating-point argument. Note that unlike the {@link
* FpUtils#copySign(float, float) copySign} method, this method * FpUtils#copySign(float, float) copySign} method, this method
* does not require NaN <code>sign</code> arguments to be treated * does not require NaN {@code sign} arguments to be treated
* as positive values; implementations are permitted to treat some * as positive values; implementations are permitted to treat some
* NaN arguments as positive and other NaN arguments as negative * NaN arguments as positive and other NaN arguments as negative
* to allow greater performance. * to allow greater performance.
* *
* @param magnitude the parameter providing the magnitude of the result * @param magnitude the parameter providing the magnitude of the result
* @param sign the parameter providing the sign of the result * @param sign the parameter providing the sign of the result
* @return a value with the magnitude of <code>magnitude</code> * @return a value with the magnitude of {@code magnitude}
* and the sign of <code>sign</code>. * and the sign of {@code sign}.
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static float rawCopySign(float magnitude, float sign) { public static float rawCopySign(float magnitude, float sign) {
...@@ -230,129 +230,129 @@ public class FpUtils { ...@@ -230,129 +230,129 @@ public class FpUtils {
/* ***************************************************************** */ /* ***************************************************************** */
/** /**
* Returns <code>true</code> if the argument is a finite * Returns {@code true} if the argument is a finite
* floating-point value; returns <code>false</code> otherwise (for * floating-point value; returns {@code false} otherwise (for
* NaN and infinity arguments). * NaN and infinity arguments).
* *
* @param d the <code>double</code> value to be tested * @param d the {@code double} value to be tested
* @return <code>true</code> if the argument is a finite * @return {@code true} if the argument is a finite
* floating-point value, <code>false</code> otherwise. * floating-point value, {@code false} otherwise.
*/ */
public static boolean isFinite(double d) { public static boolean isFinite(double d) {
return Math.abs(d) <= DoubleConsts.MAX_VALUE; return Math.abs(d) <= DoubleConsts.MAX_VALUE;
} }
/** /**
* Returns <code>true</code> if the argument is a finite * Returns {@code true} if the argument is a finite
* floating-point value; returns <code>false</code> otherwise (for * floating-point value; returns {@code false} otherwise (for
* NaN and infinity arguments). * NaN and infinity arguments).
* *
* @param f the <code>float</code> value to be tested * @param f the {@code float} value to be tested
* @return <code>true</code> if the argument is a finite * @return {@code true} if the argument is a finite
* floating-point value, <code>false</code> otherwise. * floating-point value, {@code false} otherwise.
*/ */
public static boolean isFinite(float f) { public static boolean isFinite(float f) {
return Math.abs(f) <= FloatConsts.MAX_VALUE; return Math.abs(f) <= FloatConsts.MAX_VALUE;
} }
/** /**
* Returns <code>true</code> if the specified number is infinitely * Returns {@code true} if the specified number is infinitely
* large in magnitude, <code>false</code> otherwise. * large in magnitude, {@code false} otherwise.
* *
* <p>Note that this method is equivalent to the {@link * <p>Note that this method is equivalent to the {@link
* Double#isInfinite(double) Double.isInfinite} method; the * Double#isInfinite(double) Double.isInfinite} method; the
* functionality is included in this class for convenience. * functionality is included in this class for convenience.
* *
* @param d the value to be tested. * @param d the value to be tested.
* @return <code>true</code> if the value of the argument is positive * @return {@code true} if the value of the argument is positive
* infinity or negative infinity; <code>false</code> otherwise. * infinity or negative infinity; {@code false} otherwise.
*/ */
public static boolean isInfinite(double d) { public static boolean isInfinite(double d) {
return Double.isInfinite(d); return Double.isInfinite(d);
} }
/** /**
* Returns <code>true</code> if the specified number is infinitely * Returns {@code true} if the specified number is infinitely
* large in magnitude, <code>false</code> otherwise. * large in magnitude, {@code false} otherwise.
* *
* <p>Note that this method is equivalent to the {@link * <p>Note that this method is equivalent to the {@link
* Float#isInfinite(float) Float.isInfinite} method; the * Float#isInfinite(float) Float.isInfinite} method; the
* functionality is included in this class for convenience. * functionality is included in this class for convenience.
* *
* @param f the value to be tested. * @param f the value to be tested.
* @return <code>true</code> if the argument is positive infinity or * @return {@code true} if the argument is positive infinity or
* negative infinity; <code>false</code> otherwise. * negative infinity; {@code false} otherwise.
*/ */
public static boolean isInfinite(float f) { public static boolean isInfinite(float f) {
return Float.isInfinite(f); return Float.isInfinite(f);
} }
/** /**
* Returns <code>true</code> if the specified number is a * Returns {@code true} if the specified number is a
* Not-a-Number (NaN) value, <code>false</code> otherwise. * Not-a-Number (NaN) value, {@code false} otherwise.
* *
* <p>Note that this method is equivalent to the {@link * <p>Note that this method is equivalent to the {@link
* Double#isNaN(double) Double.isNaN} method; the functionality is * Double#isNaN(double) Double.isNaN} method; the functionality is
* included in this class for convenience. * included in this class for convenience.
* *
* @param d the value to be tested. * @param d the value to be tested.
* @return <code>true</code> if the value of the argument is NaN; * @return {@code true} if the value of the argument is NaN;
* <code>false</code> otherwise. * {@code false} otherwise.
*/ */
public static boolean isNaN(double d) { public static boolean isNaN(double d) {
return Double.isNaN(d); return Double.isNaN(d);
} }
/** /**
* Returns <code>true</code> if the specified number is a * Returns {@code true} if the specified number is a
* Not-a-Number (NaN) value, <code>false</code> otherwise. * Not-a-Number (NaN) value, {@code false} otherwise.
* *
* <p>Note that this method is equivalent to the {@link * <p>Note that this method is equivalent to the {@link
* Float#isNaN(float) Float.isNaN} method; the functionality is * Float#isNaN(float) Float.isNaN} method; the functionality is
* included in this class for convenience. * included in this class for convenience.
* *
* @param f the value to be tested. * @param f the value to be tested.
* @return <code>true</code> if the argument is NaN; * @return {@code true} if the argument is NaN;
* <code>false</code> otherwise. * {@code false} otherwise.
*/ */
public static boolean isNaN(float f) { public static boolean isNaN(float f) {
return Float.isNaN(f); return Float.isNaN(f);
} }
/** /**
* Returns <code>true</code> if the unordered relation holds * Returns {@code true} if the unordered relation holds
* between the two arguments. When two floating-point values are * between the two arguments. When two floating-point values are
* unordered, one value is neither less than, equal to, nor * unordered, one value is neither less than, equal to, nor
* greater than the other. For the unordered relation to be true, * greater than the other. For the unordered relation to be true,
* at least one argument must be a <code>NaN</code>. * at least one argument must be a {@code NaN}.
* *
* @param arg1 the first argument * @param arg1 the first argument
* @param arg2 the second argument * @param arg2 the second argument
* @return <code>true</code> if at least one argument is a NaN, * @return {@code true} if at least one argument is a NaN,
* <code>false</code> otherwise. * {@code false} otherwise.
*/ */
public static boolean isUnordered(double arg1, double arg2) { public static boolean isUnordered(double arg1, double arg2) {
return isNaN(arg1) || isNaN(arg2); return isNaN(arg1) || isNaN(arg2);
} }
/** /**
* Returns <code>true</code> if the unordered relation holds * Returns {@code true} if the unordered relation holds
* between the two arguments. When two floating-point values are * between the two arguments. When two floating-point values are
* unordered, one value is neither less than, equal to, nor * unordered, one value is neither less than, equal to, nor
* greater than the other. For the unordered relation to be true, * greater than the other. For the unordered relation to be true,
* at least one argument must be a <code>NaN</code>. * at least one argument must be a {@code NaN}.
* *
* @param arg1 the first argument * @param arg1 the first argument
* @param arg2 the second argument * @param arg2 the second argument
* @return <code>true</code> if at least one argument is a NaN, * @return {@code true} if at least one argument is a NaN,
* <code>false</code> otherwise. * {@code false} otherwise.
*/ */
public static boolean isUnordered(float arg1, float arg2) { public static boolean isUnordered(float arg1, float arg2) {
return isNaN(arg1) || isNaN(arg2); return isNaN(arg1) || isNaN(arg2);
} }
/** /**
* Returns unbiased exponent of a <code>double</code>; for * Returns unbiased exponent of a {@code double}; for
* subnormal values, the number is treated as if it were * subnormal values, the number is treated as if it were
* normalized. That is for all finite, non-zero, positive numbers * normalized. That is for all finite, non-zero, positive numbers
* <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
...@@ -378,7 +378,6 @@ public class FpUtils { ...@@ -378,7 +378,6 @@ public class FpUtils {
return (1<<30); // 2^30 return (1<<30); // 2^30
else // infinite value else // infinite value
return (1<<28); // 2^28 return (1<<28); // 2^28
// break;
case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal
if(d == 0.0) { if(d == 0.0) {
...@@ -414,18 +413,16 @@ public class FpUtils { ...@@ -414,18 +413,16 @@ public class FpUtils {
exponent < DoubleConsts.MIN_EXPONENT); exponent < DoubleConsts.MIN_EXPONENT);
return exponent; return exponent;
} }
// break;
default: default:
assert( exponent >= DoubleConsts.MIN_EXPONENT && assert( exponent >= DoubleConsts.MIN_EXPONENT &&
exponent <= DoubleConsts.MAX_EXPONENT); exponent <= DoubleConsts.MAX_EXPONENT);
return exponent; return exponent;
// break;
} }
} }
/** /**
* Returns unbiased exponent of a <code>float</code>; for * Returns unbiased exponent of a {@code float}; for
* subnormal values, the number is treated as if it were * subnormal values, the number is treated as if it were
* normalized. That is for all finite, non-zero, positive numbers * normalized. That is for all finite, non-zero, positive numbers
* <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
...@@ -451,7 +448,6 @@ public class FpUtils { ...@@ -451,7 +448,6 @@ public class FpUtils {
return (1<<30); // 2^30 return (1<<30); // 2^30
else // infinite value else // infinite value
return (1<<28); // 2^28 return (1<<28); // 2^28
// break;
case FloatConsts.MIN_EXPONENT-1: // zero or subnormal case FloatConsts.MIN_EXPONENT-1: // zero or subnormal
if(f == 0.0f) { if(f == 0.0f) {
...@@ -487,13 +483,11 @@ public class FpUtils { ...@@ -487,13 +483,11 @@ public class FpUtils {
exponent < FloatConsts.MIN_EXPONENT); exponent < FloatConsts.MIN_EXPONENT);
return exponent; return exponent;
} }
// break;
default: default:
assert( exponent >= FloatConsts.MIN_EXPONENT && assert( exponent >= FloatConsts.MIN_EXPONENT &&
exponent <= FloatConsts.MAX_EXPONENT); exponent <= FloatConsts.MAX_EXPONENT);
return exponent; return exponent;
// break;
} }
} }
...@@ -534,22 +528,22 @@ public class FpUtils { ...@@ -534,22 +528,22 @@ public class FpUtils {
*/ */
/** /**
* Return <code>d</code> &times; * Return {@code d} &times;
* 2<sup><code>scale_factor</code></sup> rounded as if performed * 2<sup>{@code scale_factor}</sup> rounded as if performed
* by a single correctly rounded floating-point multiply to a * by a single correctly rounded floating-point multiply to a
* member of the double value set. See <a * member of the double value set. See <a
* href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a> * href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a>
* of the <a href="http://java.sun.com/docs/books/jls/html/">Java * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
* Language Specification</a> for a discussion of floating-point * Language Specification</a> for a discussion of floating-point
* value sets. If the exponent of the result is between the * value sets. If the exponent of the result is between the
* <code>double</code>'s minimum exponent and maximum exponent, * {@code double}'s minimum exponent and maximum exponent,
* the answer is calculated exactly. If the exponent of the * the answer is calculated exactly. If the exponent of the
* result would be larger than <code>doubles</code>'s maximum * result would be larger than {@code doubles}'s maximum
* exponent, an infinity is returned. Note that if the result is * exponent, an infinity is returned. Note that if the result is
* subnormal, precision may be lost; that is, when <code>scalb(x, * subnormal, precision may be lost; that is, when {@code scalb(x,
* n)</code> is subnormal, <code>scalb(scalb(x, n), -n)</code> may * n)} is subnormal, {@code scalb(scalb(x, n), -n)} may
* not equal <i>x</i>. When the result is non-NaN, the result has * not equal <i>x</i>. When the result is non-NaN, the result has
* the same sign as <code>d</code>. * the same sign as {@code d}.
* *
*<p> *<p>
* Special cases: * Special cases:
...@@ -562,8 +556,8 @@ public class FpUtils { ...@@ -562,8 +556,8 @@ public class FpUtils {
* </ul> * </ul>
* *
* @param d number to be scaled by a power of two. * @param d number to be scaled by a power of two.
* @param scale_factor power of 2 used to scale <code>d</code> * @param scale_factor power of 2 used to scale {@code d}
* @return <code>d * </code>2<sup><code>scale_factor</code></sup> * @return {@code d * }2<sup>{@code scale_factor}</sup>
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static double scalb(double d, int scale_factor) { public static double scalb(double d, int scale_factor) {
...@@ -644,22 +638,22 @@ public class FpUtils { ...@@ -644,22 +638,22 @@ public class FpUtils {
} }
/** /**
* Return <code>f </code>&times; * Return {@code f} &times;
* 2<sup><code>scale_factor</code></sup> rounded as if performed * 2<sup>{@code scale_factor}</sup> rounded as if performed
* by a single correctly rounded floating-point multiply to a * by a single correctly rounded floating-point multiply to a
* member of the float value set. See <a * member of the float value set. See <a
* href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a> * href="http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208">&sect;4.2.3</a>
* of the <a href="http://java.sun.com/docs/books/jls/html/">Java * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
* Language Specification</a> for a discussion of floating-point * Language Specification</a> for a discussion of floating-point
* value set. If the exponent of the result is between the * value set. If the exponent of the result is between the
* <code>float</code>'s minimum exponent and maximum exponent, the * {@code float}'s minimum exponent and maximum exponent, the
* answer is calculated exactly. If the exponent of the result * answer is calculated exactly. If the exponent of the result
* would be larger than <code>float</code>'s maximum exponent, an * would be larger than {@code float}'s maximum exponent, an
* infinity is returned. Note that if the result is subnormal, * infinity is returned. Note that if the result is subnormal,
* precision may be lost; that is, when <code>scalb(x, n)</code> * precision may be lost; that is, when {@code scalb(x, n)}
* is subnormal, <code>scalb(scalb(x, n), -n)</code> may not equal * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
* <i>x</i>. When the result is non-NaN, the result has the same * <i>x</i>. When the result is non-NaN, the result has the same
* sign as <code>f</code>. * sign as {@code f}.
* *
*<p> *<p>
* Special cases: * Special cases:
...@@ -672,8 +666,8 @@ public class FpUtils { ...@@ -672,8 +666,8 @@ public class FpUtils {
* </ul> * </ul>
* *
* @param f number to be scaled by a power of two. * @param f number to be scaled by a power of two.
* @param scale_factor power of 2 used to scale <code>f</code> * @param scale_factor power of 2 used to scale {@code f}
* @return <code>f * </code>2<sup><code>scale_factor</code></sup> * @return {@code f * }2<sup>{@code scale_factor}</sup>
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static float scalb(float f, int scale_factor) { public static float scalb(float f, int scale_factor) {
...@@ -709,34 +703,34 @@ public class FpUtils { ...@@ -709,34 +703,34 @@ public class FpUtils {
* <ul> * <ul>
* <li> If either argument is a NaN, then NaN is returned. * <li> If either argument is a NaN, then NaN is returned.
* *
* <li> If both arguments are signed zeros, <code>direction</code> * <li> If both arguments are signed zeros, {@code direction}
* is returned unchanged (as implied by the requirement of * is returned unchanged (as implied by the requirement of
* returning the second argument if the arguments compare as * returning the second argument if the arguments compare as
* equal). * equal).
* *
* <li> If <code>start</code> is * <li> If {@code start} is
* &plusmn;<code>Double.MIN_VALUE</code> and <code>direction</code> * &plusmn;{@code Double.MIN_VALUE} and {@code direction}
* has a value such that the result should have a smaller * has a value such that the result should have a smaller
* magnitude, then a zero with the same sign as <code>start</code> * magnitude, then a zero with the same sign as {@code start}
* is returned. * is returned.
* *
* <li> If <code>start</code> is infinite and * <li> If {@code start} is infinite and
* <code>direction</code> has a value such that the result should * {@code direction} has a value such that the result should
* have a smaller magnitude, <code>Double.MAX_VALUE</code> with the * have a smaller magnitude, {@code Double.MAX_VALUE} with the
* same sign as <code>start</code> is returned. * same sign as {@code start} is returned.
* *
* <li> If <code>start</code> is equal to &plusmn; * <li> If {@code start} is equal to &plusmn;
* <code>Double.MAX_VALUE</code> and <code>direction</code> has a * {@code Double.MAX_VALUE} and {@code direction} has a
* value such that the result should have a larger magnitude, an * value such that the result should have a larger magnitude, an
* infinity with same sign as <code>start</code> is returned. * infinity with same sign as {@code start} is returned.
* </ul> * </ul>
* *
* @param start starting floating-point value * @param start starting floating-point value
* @param direction value indicating which of * @param direction value indicating which of
* <code>start</code>'s neighbors or <code>start</code> should * {@code start}'s neighbors or {@code start} should
* be returned * be returned
* @return The floating-point number adjacent to <code>start</code> in the * @return The floating-point number adjacent to {@code start} in the
* direction of <code>direction</code>. * direction of {@code direction}.
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static double nextAfter(double start, double direction) { public static double nextAfter(double start, double direction) {
...@@ -809,34 +803,34 @@ public class FpUtils { ...@@ -809,34 +803,34 @@ public class FpUtils {
* <ul> * <ul>
* <li> If either argument is a NaN, then NaN is returned. * <li> If either argument is a NaN, then NaN is returned.
* *
* <li> If both arguments are signed zeros, a <code>float</code> * <li> If both arguments are signed zeros, a {@code float}
* zero with the same sign as <code>direction</code> is returned * zero with the same sign as {@code direction} is returned
* (as implied by the requirement of returning the second argument * (as implied by the requirement of returning the second argument
* if the arguments compare as equal). * if the arguments compare as equal).
* *
* <li> If <code>start</code> is * <li> If {@code start} is
* &plusmn;<code>Float.MIN_VALUE</code> and <code>direction</code> * &plusmn;{@code Float.MIN_VALUE} and {@code direction}
* has a value such that the result should have a smaller * has a value such that the result should have a smaller
* magnitude, then a zero with the same sign as <code>start</code> * magnitude, then a zero with the same sign as {@code start}
* is returned. * is returned.
* *
* <li> If <code>start</code> is infinite and * <li> If {@code start} is infinite and
* <code>direction</code> has a value such that the result should * {@code direction} has a value such that the result should
* have a smaller magnitude, <code>Float.MAX_VALUE</code> with the * have a smaller magnitude, {@code Float.MAX_VALUE} with the
* same sign as <code>start</code> is returned. * same sign as {@code start} is returned.
* *
* <li> If <code>start</code> is equal to &plusmn; * <li> If {@code start} is equal to &plusmn;
* <code>Float.MAX_VALUE</code> and <code>direction</code> has a * {@code Float.MAX_VALUE} and {@code direction} has a
* value such that the result should have a larger magnitude, an * value such that the result should have a larger magnitude, an
* infinity with same sign as <code>start</code> is returned. * infinity with same sign as {@code start} is returned.
* </ul> * </ul>
* *
* @param start starting floating-point value * @param start starting floating-point value
* @param direction value indicating which of * @param direction value indicating which of
* <code>start</code>'s neighbors or <code>start</code> should * {@code start}'s neighbors or {@code start} should
* be returned * be returned
* @return The floating-point number adjacent to <code>start</code> in the * @return The floating-point number adjacent to {@code start} in the
* direction of <code>direction</code>. * direction of {@code direction}.
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static float nextAfter(float start, double direction) { public static float nextAfter(float start, double direction) {
...@@ -900,12 +894,12 @@ public class FpUtils { ...@@ -900,12 +894,12 @@ public class FpUtils {
} }
/** /**
* Returns the floating-point value adjacent to <code>d</code> in * Returns the floating-point value adjacent to {@code d} in
* the direction of positive infinity. This method is * the direction of positive infinity. This method is
* semantically equivalent to <code>nextAfter(d, * semantically equivalent to {@code nextAfter(d,
* Double.POSITIVE_INFINITY)</code>; however, a <code>nextUp</code> * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
* implementation may run faster than its equivalent * implementation may run faster than its equivalent
* <code>nextAfter</code> call. * {@code nextAfter} call.
* *
* <p>Special Cases: * <p>Special Cases:
* <ul> * <ul>
...@@ -915,7 +909,7 @@ public class FpUtils { ...@@ -915,7 +909,7 @@ public class FpUtils {
* positive infinity. * positive infinity.
* *
* <li> If the argument is zero, the result is * <li> If the argument is zero, the result is
* <code>Double.MIN_VALUE</code> * {@code Double.MIN_VALUE}
* *
* </ul> * </ul>
* *
...@@ -935,12 +929,12 @@ public class FpUtils { ...@@ -935,12 +929,12 @@ public class FpUtils {
} }
/** /**
* Returns the floating-point value adjacent to <code>f</code> in * Returns the floating-point value adjacent to {@code f} in
* the direction of positive infinity. This method is * the direction of positive infinity. This method is
* semantically equivalent to <code>nextAfter(f, * semantically equivalent to {@code nextAfter(f,
* Double.POSITIVE_INFINITY)</code>; however, a <code>nextUp</code> * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
* implementation may run faster than its equivalent * implementation may run faster than its equivalent
* <code>nextAfter</code> call. * {@code nextAfter} call.
* *
* <p>Special Cases: * <p>Special Cases:
* <ul> * <ul>
...@@ -950,7 +944,7 @@ public class FpUtils { ...@@ -950,7 +944,7 @@ public class FpUtils {
* positive infinity. * positive infinity.
* *
* <li> If the argument is zero, the result is * <li> If the argument is zero, the result is
* <code>Float.MIN_VALUE</code> * {@code Float.MIN_VALUE}
* *
* </ul> * </ul>
* *
...@@ -970,12 +964,12 @@ public class FpUtils { ...@@ -970,12 +964,12 @@ public class FpUtils {
} }
/** /**
* Returns the floating-point value adjacent to <code>d</code> in * Returns the floating-point value adjacent to {@code d} in
* the direction of negative infinity. This method is * the direction of negative infinity. This method is
* semantically equivalent to <code>nextAfter(d, * semantically equivalent to {@code nextAfter(d,
* Double.NEGATIVE_INFINITY)</code>; however, a * Double.NEGATIVE_INFINITY)}; however, a
* <code>nextDown</code> implementation may run faster than its * {@code nextDown} implementation may run faster than its
* equivalent <code>nextAfter</code> call. * equivalent {@code nextAfter} call.
* *
* <p>Special Cases: * <p>Special Cases:
* <ul> * <ul>
...@@ -985,7 +979,7 @@ public class FpUtils { ...@@ -985,7 +979,7 @@ public class FpUtils {
* negative infinity. * negative infinity.
* *
* <li> If the argument is zero, the result is * <li> If the argument is zero, the result is
* <code>-Double.MIN_VALUE</code> * {@code -Double.MIN_VALUE}
* *
* </ul> * </ul>
* *
...@@ -1007,12 +1001,12 @@ public class FpUtils { ...@@ -1007,12 +1001,12 @@ public class FpUtils {
} }
/** /**
* Returns the floating-point value adjacent to <code>f</code> in * Returns the floating-point value adjacent to {@code f} in
* the direction of negative infinity. This method is * the direction of negative infinity. This method is
* semantically equivalent to <code>nextAfter(f, * semantically equivalent to {@code nextAfter(f,
* Float.NEGATIVE_INFINITY)</code>; however, a * Float.NEGATIVE_INFINITY)}; however, a
* <code>nextDown</code> implementation may run faster than its * {@code nextDown} implementation may run faster than its
* equivalent <code>nextAfter</code> call. * equivalent {@code nextAfter} call.
* *
* <p>Special Cases: * <p>Special Cases:
* <ul> * <ul>
...@@ -1022,7 +1016,7 @@ public class FpUtils { ...@@ -1022,7 +1016,7 @@ public class FpUtils {
* negative infinity. * negative infinity.
* *
* <li> If the argument is zero, the result is * <li> If the argument is zero, the result is
* <code>-Float.MIN_VALUE</code> * {@code -Float.MIN_VALUE}
* *
* </ul> * </ul>
* *
...@@ -1046,13 +1040,13 @@ public class FpUtils { ...@@ -1046,13 +1040,13 @@ public class FpUtils {
/** /**
* Returns the first floating-point argument with the sign of the * Returns the first floating-point argument with the sign of the
* second floating-point argument. For this method, a NaN * second floating-point argument. For this method, a NaN
* <code>sign</code> argument is always treated as if it were * {@code sign} argument is always treated as if it were
* positive. * positive.
* *
* @param magnitude the parameter providing the magnitude of the result * @param magnitude the parameter providing the magnitude of the result
* @param sign the parameter providing the sign of the result * @param sign the parameter providing the sign of the result
* @return a value with the magnitude of <code>magnitude</code> * @return a value with the magnitude of {@code magnitude}
* and the sign of <code>sign</code>. * and the sign of {@code sign}.
* @author Joseph D. Darcy * @author Joseph D. Darcy
* @since 1.5 * @since 1.5
*/ */
...@@ -1063,13 +1057,13 @@ public class FpUtils { ...@@ -1063,13 +1057,13 @@ public class FpUtils {
/** /**
* Returns the first floating-point argument with the sign of the * Returns the first floating-point argument with the sign of the
* second floating-point argument. For this method, a NaN * second floating-point argument. For this method, a NaN
* <code>sign</code> argument is always treated as if it were * {@code sign} argument is always treated as if it were
* positive. * positive.
* *
* @param magnitude the parameter providing the magnitude of the result * @param magnitude the parameter providing the magnitude of the result
* @param sign the parameter providing the sign of the result * @param sign the parameter providing the sign of the result
* @return a value with the magnitude of <code>magnitude</code> * @return a value with the magnitude of {@code magnitude}
* and the sign of <code>sign</code>. * and the sign of {@code sign}.
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
public static float copySign(float magnitude, float sign) { public static float copySign(float magnitude, float sign) {
...@@ -1078,8 +1072,8 @@ public class FpUtils { ...@@ -1078,8 +1072,8 @@ public class FpUtils {
/** /**
* Returns the size of an ulp of the argument. An ulp of a * Returns the size of an ulp of the argument. An ulp of a
* <code>double</code> value is the positive distance between this * {@code double} value is the positive distance between this
* floating-point value and the <code>double</code> value next * floating-point value and the {@code double} value next
* larger in magnitude. Note that for non-NaN <i>x</i>, * larger in magnitude. Note that for non-NaN <i>x</i>,
* <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
* *
...@@ -1089,8 +1083,8 @@ public class FpUtils { ...@@ -1089,8 +1083,8 @@ public class FpUtils {
* <li> If the argument is positive or negative infinity, then the * <li> If the argument is positive or negative infinity, then the
* result is positive infinity. * result is positive infinity.
* <li> If the argument is positive or negative zero, then the result is * <li> If the argument is positive or negative zero, then the result is
* <code>Double.MIN_VALUE</code>. * {@code Double.MIN_VALUE}.
* <li> If the argument is &plusmn;<code>Double.MAX_VALUE</code>, then * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
* the result is equal to 2<sup>971</sup>. * the result is equal to 2<sup>971</sup>.
* </ul> * </ul>
* *
...@@ -1105,11 +1099,9 @@ public class FpUtils { ...@@ -1105,11 +1099,9 @@ public class FpUtils {
switch(exp) { switch(exp) {
case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity
return Math.abs(d); return Math.abs(d);
// break;
case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal
return Double.MIN_VALUE; return Double.MIN_VALUE;
// break
default: default:
assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT; assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;
...@@ -1126,14 +1118,13 @@ public class FpUtils { ...@@ -1126,14 +1118,13 @@ public class FpUtils {
return Double.longBitsToDouble(1L << return Double.longBitsToDouble(1L <<
(exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) )); (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
} }
// break
} }
} }
/** /**
* Returns the size of an ulp of the argument. An ulp of a * Returns the size of an ulp of the argument. An ulp of a
* <code>float</code> value is the positive distance between this * {@code float} value is the positive distance between this
* floating-point value and the <code>float</code> value next * floating-point value and the {@code float} value next
* larger in magnitude. Note that for non-NaN <i>x</i>, * larger in magnitude. Note that for non-NaN <i>x</i>,
* <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
* *
...@@ -1143,8 +1134,8 @@ public class FpUtils { ...@@ -1143,8 +1134,8 @@ public class FpUtils {
* <li> If the argument is positive or negative infinity, then the * <li> If the argument is positive or negative infinity, then the
* result is positive infinity. * result is positive infinity.
* <li> If the argument is positive or negative zero, then the result is * <li> If the argument is positive or negative zero, then the result is
* <code>Float.MIN_VALUE</code>. * {@code Float.MIN_VALUE}.
* <li> If the argument is &plusmn;<code>Float.MAX_VALUE</code>, then * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
* the result is equal to 2<sup>104</sup>. * the result is equal to 2<sup>104</sup>.
* </ul> * </ul>
* *
...@@ -1159,11 +1150,9 @@ public class FpUtils { ...@@ -1159,11 +1150,9 @@ public class FpUtils {
switch(exp) { switch(exp) {
case FloatConsts.MAX_EXPONENT+1: // NaN or infinity case FloatConsts.MAX_EXPONENT+1: // NaN or infinity
return Math.abs(f); return Math.abs(f);
// break;
case FloatConsts.MIN_EXPONENT-1: // zero or subnormal case FloatConsts.MIN_EXPONENT-1: // zero or subnormal
return FloatConsts.MIN_VALUE; return FloatConsts.MIN_VALUE;
// break
default: default:
assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT; assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;
...@@ -1180,7 +1169,6 @@ public class FpUtils { ...@@ -1180,7 +1169,6 @@ public class FpUtils {
return Float.intBitsToFloat(1 << return Float.intBitsToFloat(1 <<
(exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) )); (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
} }
// break
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册