@@ -28,6 +28,7 @@ public class MathTest extends GWTTestCase {
2828
2929 private static final Integer [] ALL_INTEGER_CANDIDATES = getAllIntegerCandidates ();
3030 private static final Long [] ALL_LONG_CANDIDATES = getAllLongCandidates ();
31+ private static final double EPS = 1E-15 ;
3132
3233 @ Override
3334 public String getModuleName () {
@@ -294,6 +295,172 @@ public void testToIntExact() {
294295 }
295296 }
296297
298+ public void testLog1p () {
299+ assertEquals (Math .log (2 ), Math .log1p (1 ), EPS );
300+ assertEquals (1 , Math .log1p (1E-30 ) * 1E30 , EPS );
301+ assertEquals (-1 , Math .log1p (-1E-30 ) * 1E30 , EPS );
302+
303+ assertEquals (Math .log (2 ), Math .log1p (hideFromCompiler (1 )), EPS );
304+ assertEquals (1 , Math .log1p (hideFromCompiler (1E-30 )) * 1E30 , EPS );
305+ assertEquals (-1 , Math .log1p (hideFromCompiler (-1E-30 )) * 1E30 , EPS );
306+ }
307+
308+ public void testExpM1 () {
309+ assertEquals (Math .E - 1 , Math .expm1 (1 ), EPS );
310+ assertEquals (1 , Math .expm1 (1E-30 ) * 1E30 , EPS );
311+ assertEquals (-1 , Math .expm1 (-1E-30 ) * 1E30 , EPS );
312+
313+ assertEquals (Math .E - 1 , Math .expm1 (hideFromCompiler (1 )), EPS );
314+ assertEquals (1 , Math .expm1 (hideFromCompiler (1E-30 )) * 1E30 , EPS );
315+ assertEquals (-1 , Math .expm1 (hideFromCompiler (-1E-30 )) * 1E30 , EPS );
316+ }
317+
318+ public void testIEEEremainderWithFolding () {
319+ assertEquals (1.0 , Math .IEEEremainder (7.0 , 3.0 ), EPS );
320+ assertEquals (-1.0 , Math .IEEEremainder (8.0 , 3.0 ), EPS );
321+ assertEquals (0.0 , Math .IEEEremainder (6.0 , 3.0 ), EPS );
322+ assertEquals (0.0 , Math .IEEEremainder (9.0 , 3.0 ), EPS );
323+ assertEquals (-1.0 , Math .IEEEremainder (-7.0 , 3.0 ), EPS );
324+ assertEquals (1.0 , Math .IEEEremainder (7.0 , -3.0 ), EPS );
325+ assertEquals (-1.0 , Math .IEEEremainder (-7.0 , -3.0 ), EPS );
326+ assertEquals (0.5 , Math .IEEEremainder (2.5 , 1.0 ), EPS );
327+ assertEquals (0.5 , Math .IEEEremainder (2.5 , 2.0 ), EPS );
328+ assertEquals (0.2 , Math .IEEEremainder (5.2 , 1.0 ), EPS );
329+
330+ assertEquals (0.0 , Math .IEEEremainder (4.5 , 1.5 ), EPS );
331+ assertEquals (1.5 , Math .IEEEremainder (7.5 , 3.0 ), EPS );
332+ assertEquals (-1.5 , Math .IEEEremainder (-7.5 , -3.0 ), EPS );
333+ assertEquals (1.5 , Math .IEEEremainder (7.5 , -3.0 ), EPS );
334+ assertEquals (-1.5 , Math .IEEEremainder (-7.5 , 3.0 ), EPS );
335+ // Remainder with 0 divisor is NaN
336+ assertTrue (Double .isNaN (Math .IEEEremainder (5.0 , 0.0 )));
337+ // 0 divided by anything is 0
338+ assertEquals (0.0 , Math .IEEEremainder (0.0 , 2.0 ), EPS );
339+ // Infinity cases produce NaN
340+ assertTrue (Double .isNaN (Math .IEEEremainder (Double .POSITIVE_INFINITY , 2.0 )));
341+ assertTrue (Double .isNaN (Math .IEEEremainder (Double .NEGATIVE_INFINITY , 2.0 )));
342+ assertEquals (2 , Math .IEEEremainder (2 , Double .POSITIVE_INFINITY ), EPS );
343+ assertEquals (2 , Math .IEEEremainder (2 , Double .NEGATIVE_INFINITY ), EPS );
344+ // Any finite number divided by infinity -> same number
345+ assertEquals (5.0 , Math .IEEEremainder (5.0 , Double .POSITIVE_INFINITY ), EPS );
346+ assertTrue (Double .isNaN (Math .IEEEremainder (Double .NaN , 2.0 )));
347+ assertTrue (Double .isNaN (Math .IEEEremainder (5.0 , Double .NaN )));
348+ assertTrue (Double .isNaN (Math .IEEEremainder (Double .NaN , Double .NaN )));
349+ }
350+
351+ public void testIEEEremainder () {
352+ assertEquals (1.0 , Math .IEEEremainder (hideFromCompiler (7.0 ), 3.0 ), EPS );
353+ assertEquals (-1.0 , Math .IEEEremainder (hideFromCompiler (8.0 ), 3.0 ), EPS );
354+ assertEquals (0.0 , Math .IEEEremainder (hideFromCompiler (6.0 ), 3.0 ), EPS );
355+ assertEquals (0.0 , Math .IEEEremainder (hideFromCompiler (9.0 ), 3.0 ), EPS );
356+ assertEquals (-1.0 , Math .IEEEremainder (hideFromCompiler (-7.0 ), 3.0 ), EPS );
357+ assertEquals (1.0 , Math .IEEEremainder (hideFromCompiler (7.0 ), -3.0 ), EPS );
358+ assertEquals (-1.0 , Math .IEEEremainder (hideFromCompiler (-7.0 ), -3.0 ), EPS );
359+ assertEquals (0.5 , Math .IEEEremainder (hideFromCompiler (2.5 ), 1.0 ), EPS );
360+ assertEquals (0.5 , Math .IEEEremainder (hideFromCompiler (2.5 ), 2.0 ), EPS );
361+ assertEquals (0.2 , Math .IEEEremainder (hideFromCompiler (5.2 ), 1.0 ), EPS );
362+
363+ assertEquals (0.0 , Math .IEEEremainder (hideFromCompiler (4.5 ), 1.5 ), EPS );
364+ assertEquals (1.5 , Math .IEEEremainder (hideFromCompiler (7.5 ), 3.0 ), EPS );
365+ assertEquals (-1.5 , Math .IEEEremainder (hideFromCompiler (-7.5 ), -3.0 ), EPS );
366+ assertEquals (1.5 , Math .IEEEremainder (hideFromCompiler (7.5 ), -3.0 ), EPS );
367+ assertEquals (-1.5 , Math .IEEEremainder (hideFromCompiler (-7.5 ), 3.0 ), EPS );
368+ // Remainder with 0 divisor is NaN
369+ assertTrue (Double .isNaN (Math .IEEEremainder (hideFromCompiler (5.0 ), 0.0 )));
370+ // 0 divided by anything is 0
371+ assertEquals (0.0 , Math .IEEEremainder (hideFromCompiler (0.0 ), 2.0 ), EPS );
372+ // Infinity cases produce NaN
373+ assertTrue (Double .isNaN (Math .IEEEremainder (hideFromCompiler (Double .POSITIVE_INFINITY ), 2.0 )));
374+ assertTrue (Double .isNaN (Math .IEEEremainder (hideFromCompiler (Double .NEGATIVE_INFINITY ), 2.0 )));
375+ assertEquals (2 , Math .IEEEremainder (hideFromCompiler (2 ), Double .POSITIVE_INFINITY ), EPS );
376+ assertEquals (2 , Math .IEEEremainder (hideFromCompiler (2 ), Double .NEGATIVE_INFINITY ), EPS );
377+ // Any finite number divided by infinity -> same number
378+ assertEquals (5.0 , Math .IEEEremainder (hideFromCompiler (5.0 ), Double .POSITIVE_INFINITY ), EPS );
379+ assertTrue (Double .isNaN (Math .IEEEremainder (hideFromCompiler (Double .NaN ), 2.0 )));
380+ assertTrue (Double .isNaN (Math .IEEEremainder (hideFromCompiler (5.0 ), Double .NaN )));
381+ assertTrue (Double .isNaN (Math .IEEEremainder (hideFromCompiler (Double .NaN ), Double .NaN )));
382+ }
383+
384+ public void testHypot () {
385+ assertEquals (5.0 , Math .hypot (3.0 , 4.0 ), EPS );
386+ assertEquals (Double .POSITIVE_INFINITY , Math .hypot (1 , Double .NEGATIVE_INFINITY ));
387+ assertEquals (Double .POSITIVE_INFINITY , Math .hypot (Double .POSITIVE_INFINITY , 1 ));
388+ assertEquals (Double .POSITIVE_INFINITY , Math .hypot (Double .NaN , Double .NEGATIVE_INFINITY ));
389+
390+ assertEquals (5.0 , Math .hypot (hideFromCompiler (3.0 ), 4.0 ), EPS );
391+ assertEquals (Double .POSITIVE_INFINITY , Math .hypot (hideFromCompiler (-1 ),
392+ Double .NEGATIVE_INFINITY ));
393+ assertEquals (Double .POSITIVE_INFINITY , Math .hypot (hideFromCompiler (Double .POSITIVE_INFINITY ),
394+ 1 ));
395+ assertEquals (Double .POSITIVE_INFINITY , Math .hypot (hideFromCompiler (Double .NaN ),
396+ Double .NEGATIVE_INFINITY ));
397+ }
398+
399+ public void testGetExponentWithFolding () {
400+ assertEquals (1 , Math .getExponent (2d ));
401+ assertEquals (1 , Math .getExponent (3d ));
402+ assertEquals (2 , Math .getExponent (4d ));
403+ assertEquals (-1023 , Math .getExponent (0d ));
404+ assertEquals (1023 , Math .getExponent (Math .pow (2 , 1023 )));
405+ assertEquals (-1023 , Math .getExponent (Math .pow (2 , -1023 )));
406+ assertEquals (1023 , Math .getExponent (-Math .pow (2 , 1023 )));
407+ assertEquals (-1023 , Math .getExponent (-Math .pow (2 , -1023 )));
408+ assertEquals (1024 , Math .getExponent (Double .POSITIVE_INFINITY ));
409+ assertEquals (1024 , Math .getExponent (Double .NEGATIVE_INFINITY ));
410+ assertEquals (1024 , Math .getExponent (Double .NaN ));
411+
412+ assertEquals (2 , Math .getExponent (4f ));
413+ assertEquals (-127 , Math .getExponent (0f ));
414+
415+ assertEquals (126 , Math .getExponent ((float ) Math .pow (2 , 126 )));
416+ assertEquals (-126 , Math .getExponent ((float ) Math .pow (2 , -126 )));
417+ assertEquals (126 , Math .getExponent ((float ) -Math .pow (2 , 126 )));
418+ assertEquals (-126 , Math .getExponent ((float ) -Math .pow (2 , -126 )));
419+ assertEquals (128 , Math .getExponent ((float ) Math .pow (2 , 500 )));
420+ assertEquals (-127 , Math .getExponent ((float ) Math .pow (2 , -500 )));
421+ }
422+
423+ public void testGetExponent () {
424+ assertEquals (1 , Math .getExponent (hideFromCompiler (2d )));
425+ assertEquals (1 , Math .getExponent (hideFromCompiler (3d )));
426+ assertEquals (2 , Math .getExponent (hideFromCompiler (4d )));
427+ assertEquals (-1023 , Math .getExponent (hideFromCompiler (0d )));
428+ assertEquals (1023 , Math .getExponent (hideFromCompiler (Math .pow (2 , 1023 ))));
429+ assertEquals (-1023 , Math .getExponent (hideFromCompiler (Math .pow (2 , -1023 ))));
430+ assertEquals (1023 , Math .getExponent (hideFromCompiler (-Math .pow (2 , 1023 ))));
431+ assertEquals (-1023 , Math .getExponent (hideFromCompiler (-Math .pow (2 , -1023 ))));
432+ assertEquals (1024 , Math .getExponent (hideFromCompiler (Double .POSITIVE_INFINITY )));
433+ assertEquals (1024 , Math .getExponent (hideFromCompiler (Double .NEGATIVE_INFINITY )));
434+ assertEquals (1024 , Math .getExponent (hideFromCompiler (Double .NaN )));
435+
436+ assertEquals (2 , Math .getExponent (hideFromCompiler (4f )));
437+ assertEquals (-127 , Math .getExponent (hideFromCompiler (0f )));
438+
439+ assertEquals (126 , Math .getExponent (hideFromCompiler ((float ) Math .pow (2 , 126 ))));
440+ assertEquals (-126 , Math .getExponent (hideFromCompiler ((float ) Math .pow (2 , -126 ))));
441+ assertEquals (126 , Math .getExponent (hideFromCompiler ((float ) -Math .pow (2 , 126 ))));
442+ assertEquals (-126 , Math .getExponent (hideFromCompiler ((float ) -Math .pow (2 , -126 ))));
443+ assertEquals (128 , Math .getExponent (hideFromCompiler ((float ) Math .pow (2 , 500 ))));
444+ assertEquals (-127 , Math .getExponent (hideFromCompiler ((float ) Math .pow (2 , -500 ))));
445+ }
446+
447+ private <T > T hideFromCompiler (T value ) {
448+ if (Math .random () < -1 ) {
449+ // Can never happen, but fools the compiler enough not to optimize this call.
450+ fail ();
451+ }
452+ return value ;
453+ }
454+
455+ private void assertThrowsArithmetic (Runnable check ) {
456+ try {
457+ check .run ();
458+ fail ("Should have failed" );
459+ } catch (ArithmeticException ex ) {
460+ // good
461+ }
462+ }
463+
297464 private static boolean fitsInInt (BigInteger big ) {
298465 return big .bitLength () < Integer .SIZE ;
299466 }
0 commit comments