@@ -186,7 +186,7 @@ private static void checkNegate() {
186186 for (var testCase : testCases ) {
187187 float arg = testCase [0 ];
188188 float expected = testCase [1 ];
189- Float16 result = negate (valueOf (arg ));
189+ Float16 result = negate (valueOfExact (arg ));
190190
191191 if (Float .compare (expected , result .floatValue ()) != 0 ) {
192192 checkFloat16 (result , expected , "negate(" + arg + ")" );
@@ -213,7 +213,7 @@ private static void checkAbs() {
213213 for (var testCase : testCases ) {
214214 float arg = testCase [0 ];
215215 float expected = testCase [1 ];
216- Float16 result = abs (valueOf (arg ));
216+ Float16 result = abs (valueOfExact (arg ));
217217
218218 if (Float .compare (expected , result .floatValue ()) != 0 ) {
219219 checkFloat16 (result , expected , "abs(" + arg + ")" );
@@ -238,7 +238,7 @@ private static void checkIsNaN() {
238238 };
239239
240240 for (var testCase : testCases ) {
241- boolean result = isNaN (valueOf (testCase ));
241+ boolean result = isNaN (valueOfExact (testCase ));
242242 if (result ) {
243243 throwRE ("isNaN returned true for " + testCase );
244244 }
@@ -254,8 +254,8 @@ private static void checkFiniteness() {
254254 };
255255
256256 for (var infinity : infinities ) {
257- boolean result1 = isFinite (valueOf (infinity ));
258- boolean result2 = isInfinite (valueOf (infinity ));
257+ boolean result1 = isFinite (valueOfExact (infinity ));
258+ boolean result2 = isInfinite (valueOfExact (infinity ));
259259
260260 if (result1 ) {
261261 throwRE ("Float16.isFinite returned true for " + infinity );
@@ -282,8 +282,8 @@ private static void checkFiniteness() {
282282 };
283283
284284 for (var finity : finities ) {
285- boolean result1 = isFinite (valueOf (finity ));
286- boolean result2 = isInfinite (valueOf (finity ));
285+ boolean result1 = isFinite (valueOfExact (finity ));
286+ boolean result2 = isInfinite (valueOfExact (finity ));
287287
288288 if (!result1 ) {
289289 throwRE ("Float16.isFinite returned true for " + finity );
@@ -301,12 +301,12 @@ private static void checkMinMax() {
301301 float small = 1.0f ;
302302 float large = 2.0f ;
303303
304- if (min (valueOf (small ), valueOf (large )).floatValue () != small ) {
304+ if (min (valueOfExact (small ), valueOfExact (large )).floatValue () != small ) {
305305 throwRE (String .format ("min(%g, %g) not equal to %g)" ,
306306 small , large , small ));
307307 }
308308
309- if (max (valueOf (small ), valueOf (large )).floatValue () != large ) {
309+ if (max (valueOfExact (small ), valueOfExact (large )).floatValue () != large ) {
310310 throwRE (String .format ("max(%g, %g) not equal to %g)" ,
311311 small , large , large ));
312312 }
@@ -318,10 +318,10 @@ private static void checkMinMax() {
318318 */
319319 private static void checkArith () {
320320 float a = 1.0f ;
321- Float16 a16 = valueOf (a );
321+ Float16 a16 = valueOfExact (a );
322322
323323 float b = 2.0f ;
324- Float16 b16 = valueOf (b );
324+ Float16 b16 = valueOfExact (b );
325325
326326 if (add (a16 , b16 ).floatValue () != (a + b )) {
327327 throwRE ("failure with " + a16 + " + " + b16 );
@@ -371,7 +371,7 @@ private static void checkSqrt() {
371371 for (var testCase : testCases ) {
372372 float arg = testCase [0 ];
373373 float expected = testCase [1 ];
374- Float16 result = sqrt (valueOf (arg ));
374+ Float16 result = sqrt (valueOfExact (arg ));
375375
376376 if (Float .compare (expected , result .floatValue ()) != 0 ) {
377377 checkFloat16 (result , expected , "sqrt(" + arg + ")" );
@@ -409,7 +409,7 @@ private static void checkGetExponent() {
409409 float arg = testCase [0 ];
410410 float expected = testCase [1 ];
411411 // Exponents are in-range for Float16
412- Float16 result = valueOf (getExponent (valueOf (arg )));
412+ Float16 result = valueOfExact (getExponent (valueOfExact (arg )));
413413
414414 if (Float .compare (expected , result .floatValue ()) != 0 ) {
415415 checkFloat16 (result , expected , "getExponent(" + arg + ")" );
@@ -445,7 +445,7 @@ private static void checkUlp() {
445445 float arg = testCase [0 ];
446446 float expected = testCase [1 ];
447447 // Exponents are in-range for Float16
448- Float16 result = ulp (valueOf (arg ));
448+ Float16 result = ulp (valueOfExact (arg ));
449449
450450 if (Float .compare (expected , result .floatValue ()) != 0 ) {
451451 checkFloat16 (result , expected , "ulp(" + arg + ")" );
@@ -602,7 +602,7 @@ private static void checkValueOfString() {
602602 String input = testCase .input ();
603603 float expected = testCase .expected ();
604604 Float16 result = Float16 .valueOf (input );
605- checkFloat16 (result , expected , "Float16.valueOf (String) " + input );
605+ checkFloat16 (result , expected , "Float16.valueOfExact (String) " + input );
606606 }
607607
608608 List <String > negativeCases = List .of ("0x1" ,
@@ -747,7 +747,7 @@ private static void testZeroes() {
747747 }
748748
749749 private static void testSimple () {
750- final float ulpOneFp16 = ulp (valueOf (1.0f )).floatValue ();
750+ final float ulpOneFp16 = ulp (valueOfExact (1.0f )).floatValue ();
751751
752752 float [][] testCases = {
753753 {1.0f , 2.0f , 3.0f ,
@@ -781,7 +781,7 @@ private static void testSimple() {
781781 }
782782
783783 private static void testRounding () {
784- final float ulpOneFp16 = ulp (valueOf (1.0f )).floatValue ();
784+ final float ulpOneFp16 = ulp (valueOfExact (1.0f )).floatValue ();
785785
786786 float [][] testCases = {
787787 // The product is equal to
@@ -844,10 +844,10 @@ private static void testRounding() {
844844 }
845845
846846 private static void testFusedMacCase (float input1 , float input2 , float input3 , float expected ) {
847- Float16 a = valueOf (input1 );
848- Float16 b = valueOf (input2 );
849- Float16 c = valueOf (input3 );
850- Float16 d = valueOf (expected );
847+ Float16 a = valueOfExact (input1 );
848+ Float16 b = valueOfExact (input2 );
849+ Float16 c = valueOfExact (input3 );
850+ Float16 d = valueOfExact (expected );
851851
852852 test ("Float16.fma(float)" , a , b , c , Float16 .fma (a , b , c ), d );
853853
@@ -870,4 +870,20 @@ private static void test(String testName,
870870 throw new RuntimeException ();
871871 }
872872 }
873+
874+ /**
875+ * {@return a Float16 value converted from the {@code float}
876+ * argument throwing an {@code ArithmeticException} if the
877+ * conversion is inexact}.
878+ *
879+ * @param f the {@code float} value to convert exactly
880+ * @throws ArithmeticException
881+ */
882+ private static Float16 valueOfExact (float f ) {
883+ Float16 f16 = valueOf (f );
884+ if (Float .compare (f16 .floatValue (), f ) != 0 ) {
885+ throw new ArithmeticException ("Inexact conversion to Float16 of float value " + f );
886+ }
887+ return f16 ;
888+ }
873889}
0 commit comments