@@ -186,7 +186,7 @@ private static void checkNegate() {
186
186
for (var testCase : testCases ) {
187
187
float arg = testCase [0 ];
188
188
float expected = testCase [1 ];
189
- Float16 result = negate (valueOf (arg ));
189
+ Float16 result = negate (valueOfExact (arg ));
190
190
191
191
if (Float .compare (expected , result .floatValue ()) != 0 ) {
192
192
checkFloat16 (result , expected , "negate(" + arg + ")" );
@@ -213,7 +213,7 @@ private static void checkAbs() {
213
213
for (var testCase : testCases ) {
214
214
float arg = testCase [0 ];
215
215
float expected = testCase [1 ];
216
- Float16 result = abs (valueOf (arg ));
216
+ Float16 result = abs (valueOfExact (arg ));
217
217
218
218
if (Float .compare (expected , result .floatValue ()) != 0 ) {
219
219
checkFloat16 (result , expected , "abs(" + arg + ")" );
@@ -238,7 +238,7 @@ private static void checkIsNaN() {
238
238
};
239
239
240
240
for (var testCase : testCases ) {
241
- boolean result = isNaN (valueOf (testCase ));
241
+ boolean result = isNaN (valueOfExact (testCase ));
242
242
if (result ) {
243
243
throwRE ("isNaN returned true for " + testCase );
244
244
}
@@ -254,8 +254,8 @@ private static void checkFiniteness() {
254
254
};
255
255
256
256
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 ));
259
259
260
260
if (result1 ) {
261
261
throwRE ("Float16.isFinite returned true for " + infinity );
@@ -282,8 +282,8 @@ private static void checkFiniteness() {
282
282
};
283
283
284
284
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 ));
287
287
288
288
if (!result1 ) {
289
289
throwRE ("Float16.isFinite returned true for " + finity );
@@ -301,12 +301,12 @@ private static void checkMinMax() {
301
301
float small = 1.0f ;
302
302
float large = 2.0f ;
303
303
304
- if (min (valueOf (small ), valueOf (large )).floatValue () != small ) {
304
+ if (min (valueOfExact (small ), valueOfExact (large )).floatValue () != small ) {
305
305
throwRE (String .format ("min(%g, %g) not equal to %g)" ,
306
306
small , large , small ));
307
307
}
308
308
309
- if (max (valueOf (small ), valueOf (large )).floatValue () != large ) {
309
+ if (max (valueOfExact (small ), valueOfExact (large )).floatValue () != large ) {
310
310
throwRE (String .format ("max(%g, %g) not equal to %g)" ,
311
311
small , large , large ));
312
312
}
@@ -318,10 +318,10 @@ private static void checkMinMax() {
318
318
*/
319
319
private static void checkArith () {
320
320
float a = 1.0f ;
321
- Float16 a16 = valueOf (a );
321
+ Float16 a16 = valueOfExact (a );
322
322
323
323
float b = 2.0f ;
324
- Float16 b16 = valueOf (b );
324
+ Float16 b16 = valueOfExact (b );
325
325
326
326
if (add (a16 , b16 ).floatValue () != (a + b )) {
327
327
throwRE ("failure with " + a16 + " + " + b16 );
@@ -371,7 +371,7 @@ private static void checkSqrt() {
371
371
for (var testCase : testCases ) {
372
372
float arg = testCase [0 ];
373
373
float expected = testCase [1 ];
374
- Float16 result = sqrt (valueOf (arg ));
374
+ Float16 result = sqrt (valueOfExact (arg ));
375
375
376
376
if (Float .compare (expected , result .floatValue ()) != 0 ) {
377
377
checkFloat16 (result , expected , "sqrt(" + arg + ")" );
@@ -409,7 +409,7 @@ private static void checkGetExponent() {
409
409
float arg = testCase [0 ];
410
410
float expected = testCase [1 ];
411
411
// Exponents are in-range for Float16
412
- Float16 result = valueOf (getExponent (valueOf (arg )));
412
+ Float16 result = valueOfExact (getExponent (valueOfExact (arg )));
413
413
414
414
if (Float .compare (expected , result .floatValue ()) != 0 ) {
415
415
checkFloat16 (result , expected , "getExponent(" + arg + ")" );
@@ -445,7 +445,7 @@ private static void checkUlp() {
445
445
float arg = testCase [0 ];
446
446
float expected = testCase [1 ];
447
447
// Exponents are in-range for Float16
448
- Float16 result = ulp (valueOf (arg ));
448
+ Float16 result = ulp (valueOfExact (arg ));
449
449
450
450
if (Float .compare (expected , result .floatValue ()) != 0 ) {
451
451
checkFloat16 (result , expected , "ulp(" + arg + ")" );
@@ -602,7 +602,7 @@ private static void checkValueOfString() {
602
602
String input = testCase .input ();
603
603
float expected = testCase .expected ();
604
604
Float16 result = Float16 .valueOf (input );
605
- checkFloat16 (result , expected , "Float16.valueOf (String) " + input );
605
+ checkFloat16 (result , expected , "Float16.valueOfExact (String) " + input );
606
606
}
607
607
608
608
List <String > negativeCases = List .of ("0x1" ,
@@ -747,7 +747,7 @@ private static void testZeroes() {
747
747
}
748
748
749
749
private static void testSimple () {
750
- final float ulpOneFp16 = ulp (valueOf (1.0f )).floatValue ();
750
+ final float ulpOneFp16 = ulp (valueOfExact (1.0f )).floatValue ();
751
751
752
752
float [][] testCases = {
753
753
{1.0f , 2.0f , 3.0f ,
@@ -781,7 +781,7 @@ private static void testSimple() {
781
781
}
782
782
783
783
private static void testRounding () {
784
- final float ulpOneFp16 = ulp (valueOf (1.0f )).floatValue ();
784
+ final float ulpOneFp16 = ulp (valueOfExact (1.0f )).floatValue ();
785
785
786
786
float [][] testCases = {
787
787
// The product is equal to
@@ -839,10 +839,10 @@ private static void testRounding() {
839
839
}
840
840
841
841
private static void testFusedMacCase (float input1 , float input2 , float input3 , float expected ) {
842
- Float16 a = valueOf (input1 );
843
- Float16 b = valueOf (input2 );
844
- Float16 c = valueOf (input3 );
845
- Float16 d = valueOf (expected );
842
+ Float16 a = valueOfExact (input1 );
843
+ Float16 b = valueOfExact (input2 );
844
+ Float16 c = valueOfExact (input3 );
845
+ Float16 d = valueOfExact (expected );
846
846
847
847
test ("Float16.fma(float)" , a , b , c , Float16 .fma (a , b , c ), d );
848
848
@@ -865,4 +865,20 @@ private static void test(String testName,
865
865
throw new RuntimeException ();
866
866
}
867
867
}
868
+
869
+ /**
870
+ * {@return a Float16 value converted from the {@code float}
871
+ * argument throwing an {@code ArithmeticException} if the
872
+ * conversion is inexact}.
873
+ *
874
+ * @param f the {@code float} value to convert exactly
875
+ * @throws ArithmeticException
876
+ */
877
+ private static Float16 valueOfExact (float f ) {
878
+ Float16 f16 = valueOf (f );
879
+ if (Float .compare (f16 .floatValue (), f ) != 0 ) {
880
+ throw new ArithmeticException ("Inexact conversion to Float16 of float value " + f );
881
+ }
882
+ return f16 ;
883
+ }
868
884
}
0 commit comments