test/java/lang/Math/Tests.java

Print this page




  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * Shared static test methods for numerical tests.  Sharing these
  26  * helper test methods avoids repeated functions in the various test
  27  * programs.  The test methods return 1 for a test failure and 0 for
  28  * success.  The order of arguments to the test methods is generally
  29  * the test name, followed by the test arguments, the computed result,
  30  * and finally the expected result.
  31  */
  32 
  33 import sun.misc.FloatConsts;
  34 import sun.misc.DoubleConsts;
  35 
  36 public class Tests {
  37     private Tests(){}; // do not instantiate
  38 
  39     public static String toHexString(float f) {
  40         if (!Float.isNaN(f))
  41             return Float.toHexString(f);
  42         else
  43             return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";
  44     }
  45 
  46     public static String toHexString(double d) {
  47         if (!Double.isNaN(d))
  48             return Double.toHexString(d);
  49         else
  50             return "NaN(0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + ")";
  51     }
  52 
  53     /**
  54      * Return the floating-point value next larger in magnitude.


  64      * Returns unbiased exponent of a {@code float}; for
  65      * subnormal values, the number is treated as if it were
  66      * normalized.  That is for all finite, non-zero, positive numbers
  67      * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
  68      * always in the range [1, 2).
  69      * <p>
  70      * Special cases:
  71      * <ul>
  72      * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
  73      * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
  74      * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
  75      * </ul>
  76      *
  77      * @param f floating-point number whose exponent is to be extracted
  78      * @return unbiased exponent of the argument.
  79      */
  80     public static int ilogb(double d) {
  81         int exponent = Math.getExponent(d);
  82 
  83         switch (exponent) {
  84         case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
  85             if( Double.isNaN(d) )
  86                 return (1<<30);         // 2^30
  87             else // infinite value
  88                 return (1<<28);         // 2^28
  89 
  90         case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
  91             if(d == 0.0) {
  92                 return -(1<<28);        // -(2^28)
  93             }
  94             else {
  95                 long transducer = Double.doubleToRawLongBits(d);
  96 
  97                 /*
  98                  * To avoid causing slow arithmetic on subnormals,
  99                  * the scaling to determine when d's significand
 100                  * is normalized is done in integer arithmetic.
 101                  * (there must be at least one "1" bit in the
 102                  * significand since zero has been screened out.
 103                  */
 104 
 105                 // isolate significand bits
 106                 transducer &= DoubleConsts.SIGNIF_BIT_MASK;
 107                 assert(transducer != 0L);
 108 
 109                 // This loop is simple and functional. We might be
 110                 // able to do something more clever that was faster;
 111                 // e.g. number of leading zero detection on
 112                 // (transducer << (# exponent and sign bits).
 113                 while (transducer <
 114                        (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
 115                     transducer *= 2;
 116                     exponent--;
 117                 }
 118                 exponent++;
 119                 assert( exponent >=
 120                         DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
 121                         exponent < DoubleConsts.MIN_EXPONENT);
 122                 return exponent;
 123             }
 124 
 125         default:
 126             assert( exponent >= DoubleConsts.MIN_EXPONENT &&
 127                     exponent <= DoubleConsts.MAX_EXPONENT);
 128             return exponent;
 129         }
 130     }
 131 
 132     /**
 133      * Returns unbiased exponent of a {@code float}; for
 134      * subnormal values, the number is treated as if it were
 135      * normalized.  That is for all finite, non-zero, positive numbers
 136      * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 137      * always in the range [1, 2).
 138      * <p>
 139      * Special cases:
 140      * <ul>
 141      * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 142      * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 143      * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 144      * </ul>
 145      *
 146      * @param f floating-point number whose exponent is to be extracted
 147      * @return unbiased exponent of the argument.
 148      */
 149      public static int ilogb(float f) {
 150         int exponent = Math.getExponent(f);
 151 
 152         switch (exponent) {
 153         case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
 154             if( Float.isNaN(f) )
 155                 return (1<<30);         // 2^30
 156             else // infinite value
 157                 return (1<<28);         // 2^28
 158 
 159         case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
 160             if(f == 0.0f) {
 161                 return -(1<<28);        // -(2^28)
 162             }
 163             else {
 164                 int transducer = Float.floatToRawIntBits(f);
 165 
 166                 /*
 167                  * To avoid causing slow arithmetic on subnormals,
 168                  * the scaling to determine when f's significand
 169                  * is normalized is done in integer arithmetic.
 170                  * (there must be at least one "1" bit in the
 171                  * significand since zero has been screened out.
 172                  */
 173 
 174                 // isolate significand bits
 175                 transducer &= FloatConsts.SIGNIF_BIT_MASK;
 176                 assert(transducer != 0);
 177 
 178                 // This loop is simple and functional. We might be
 179                 // able to do something more clever that was faster;
 180                 // e.g. number of leading zero detection on
 181                 // (transducer << (# exponent and sign bits).
 182                 while (transducer <
 183                        (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
 184                     transducer *= 2;
 185                     exponent--;
 186                 }
 187                 exponent++;
 188                 assert( exponent >=
 189                         FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
 190                         exponent < FloatConsts.MIN_EXPONENT);
 191                 return exponent;
 192             }
 193 
 194         default:
 195             assert( exponent >= FloatConsts.MIN_EXPONENT &&
 196                     exponent <= FloatConsts.MAX_EXPONENT);
 197             return exponent;
 198         }
 199     }
 200 
 201     /**
 202      * Returns {@code true} if the unordered relation holds
 203      * between the two arguments.  When two floating-point values are
 204      * unordered, one value is neither less than, equal to, nor
 205      * greater than the other.  For the unordered relation to be true,
 206      * at least one argument must be a {@code NaN}.
 207      *
 208      * @param arg1      the first argument
 209      * @param arg2      the second argument
 210      * @return {@code true} if at least one argument is a NaN,
 211      * {@code false} otherwise.
 212      */
 213      public static boolean isUnordered(float arg1, float arg2) {
 214         return Float.isNaN(arg1) || Float.isNaN(arg2);
 215     }
 216 




  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * Shared static test methods for numerical tests.  Sharing these
  26  * helper test methods avoids repeated functions in the various test
  27  * programs.  The test methods return 1 for a test failure and 0 for
  28  * success.  The order of arguments to the test methods is generally
  29  * the test name, followed by the test arguments, the computed result,
  30  * and finally the expected result.
  31  */
  32 
  33 import jdk.testlibrary.DoubleUtils;
  34 import jdk.testlibrary.FloatUtils;
  35 
  36 public class Tests {
  37     private Tests(){}; // do not instantiate
  38 
  39     public static String toHexString(float f) {
  40         if (!Float.isNaN(f))
  41             return Float.toHexString(f);
  42         else
  43             return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";
  44     }
  45 
  46     public static String toHexString(double d) {
  47         if (!Double.isNaN(d))
  48             return Double.toHexString(d);
  49         else
  50             return "NaN(0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + ")";
  51     }
  52 
  53     /**
  54      * Return the floating-point value next larger in magnitude.


  64      * Returns unbiased exponent of a {@code float}; for
  65      * subnormal values, the number is treated as if it were
  66      * normalized.  That is for all finite, non-zero, positive numbers
  67      * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
  68      * always in the range [1, 2).
  69      * <p>
  70      * Special cases:
  71      * <ul>
  72      * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
  73      * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
  74      * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
  75      * </ul>
  76      *
  77      * @param f floating-point number whose exponent is to be extracted
  78      * @return unbiased exponent of the argument.
  79      */
  80     public static int ilogb(double d) {
  81         int exponent = Math.getExponent(d);
  82 
  83         switch (exponent) {
  84         case Double.MAX_EXPONENT+1:       // NaN or infinity
  85             if( Double.isNaN(d) )
  86                 return (1<<30);         // 2^30
  87             else // infinite value
  88                 return (1<<28);         // 2^28
  89 
  90         case Double.MIN_EXPONENT-1:       // zero or subnormal
  91             if(d == 0.0) {
  92                 return -(1<<28);        // -(2^28)
  93             }
  94             else {
  95                 long transducer = Double.doubleToRawLongBits(d);
  96 
  97                 /*
  98                  * To avoid causing slow arithmetic on subnormals,
  99                  * the scaling to determine when d's significand
 100                  * is normalized is done in integer arithmetic.
 101                  * (there must be at least one "1" bit in the
 102                  * significand since zero has been screened out.
 103                  */
 104 
 105                 // isolate significand bits
 106                 transducer &= DoubleUtils.SIGNIF_BIT_MASK;
 107                 assert(transducer != 0L);
 108 
 109                 // This loop is simple and functional. We might be
 110                 // able to do something more clever that was faster;
 111                 // e.g. number of leading zero detection on
 112                 // (transducer << (# exponent and sign bits).
 113                 while (transducer <
 114                        (1L << (DoubleUtils.SIGNIFICAND_WIDTH - 1))) {
 115                     transducer *= 2;
 116                     exponent--;
 117                 }
 118                 exponent++;
 119                 assert( exponent >=
 120                         Double.MIN_EXPONENT - (DoubleUtils.SIGNIFICAND_WIDTH-1) &&
 121                         exponent < Double.MIN_EXPONENT);
 122                 return exponent;
 123             }
 124 
 125         default:
 126             assert( exponent >= Double.MIN_EXPONENT &&
 127                     exponent <= Double.MAX_EXPONENT);
 128             return exponent;
 129         }
 130     }
 131 
 132     /**
 133      * Returns unbiased exponent of a {@code float}; for
 134      * subnormal values, the number is treated as if it were
 135      * normalized.  That is for all finite, non-zero, positive numbers
 136      * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 137      * always in the range [1, 2).
 138      * <p>
 139      * Special cases:
 140      * <ul>
 141      * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 142      * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 143      * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 144      * </ul>
 145      *
 146      * @param f floating-point number whose exponent is to be extracted
 147      * @return unbiased exponent of the argument.
 148      */
 149      public static int ilogb(float f) {
 150         int exponent = Math.getExponent(f);
 151 
 152         switch (exponent) {
 153         case Float.MAX_EXPONENT+1:        // NaN or infinity
 154             if( Float.isNaN(f) )
 155                 return (1<<30);         // 2^30
 156             else // infinite value
 157                 return (1<<28);         // 2^28
 158 
 159         case Float.MIN_EXPONENT-1:        // zero or subnormal
 160             if(f == 0.0f) {
 161                 return -(1<<28);        // -(2^28)
 162             }
 163             else {
 164                 int transducer = Float.floatToRawIntBits(f);
 165 
 166                 /*
 167                  * To avoid causing slow arithmetic on subnormals,
 168                  * the scaling to determine when f's significand
 169                  * is normalized is done in integer arithmetic.
 170                  * (there must be at least one "1" bit in the
 171                  * significand since zero has been screened out.
 172                  */
 173 
 174                 // isolate significand bits
 175                 transducer &= FloatUtils.SIGNIF_BIT_MASK;
 176                 assert(transducer != 0);
 177 
 178                 // This loop is simple and functional. We might be
 179                 // able to do something more clever that was faster;
 180                 // e.g. number of leading zero detection on
 181                 // (transducer << (# exponent and sign bits).
 182                 while (transducer <
 183                        (1 << (FloatUtils.SIGNIFICAND_WIDTH - 1))) {
 184                     transducer *= 2;
 185                     exponent--;
 186                 }
 187                 exponent++;
 188                 assert( exponent >=
 189                         Float.MIN_EXPONENT - (FloatUtils.SIGNIFICAND_WIDTH-1) &&
 190                         exponent < Float.MIN_EXPONENT);
 191                 return exponent;
 192             }
 193 
 194         default:
 195             assert( exponent >= Float.MIN_EXPONENT &&
 196                     exponent <= Float.MAX_EXPONENT);
 197             return exponent;
 198         }
 199     }
 200 
 201     /**
 202      * Returns {@code true} if the unordered relation holds
 203      * between the two arguments.  When two floating-point values are
 204      * unordered, one value is neither less than, equal to, nor
 205      * greater than the other.  For the unordered relation to be true,
 206      * at least one argument must be a {@code NaN}.
 207      *
 208      * @param arg1      the first argument
 209      * @param arg2      the second argument
 210      * @return {@code true} if at least one argument is a NaN,
 211      * {@code false} otherwise.
 212      */
 213      public static boolean isUnordered(float arg1, float arg2) {
 214         return Float.isNaN(arg1) || Float.isNaN(arg2);
 215     }
 216