@@ -8,19 +8,22 @@ assertTrue(isNaN(Math.expm1(NaN)));
88assertTrue ( isNaN ( Math . expm1 ( function ( ) { } ) ) ) ;
99assertTrue ( isNaN ( Math . expm1 ( { toString : function ( ) { return NaN ; } } ) ) ) ;
1010assertTrue ( isNaN ( Math . expm1 ( { valueOf : function ( ) { return "abc" ; } } ) ) ) ;
11- assertEquals ( " Infinity" , String ( 1 / Math . expm1 ( 0 ) ) ) ;
12- assertEquals ( " -Infinity" , String ( 1 / Math . expm1 ( - 0 ) ) ) ;
13- assertEquals ( " Infinity" , String ( Math . expm1 ( Infinity ) ) ) ;
11+ assertEquals ( Infinity , 1 / Math . expm1 ( 0 ) ) ;
12+ assertEquals ( - Infinity , 1 / Math . expm1 ( - 0 ) ) ;
13+ assertEquals ( Infinity , Math . expm1 ( Infinity ) ) ;
1414assertEquals ( - 1 , Math . expm1 ( - Infinity ) ) ;
1515
16- for ( var x = 0.1 ; x < 700 ; x += 0.1 ) {
16+
17+ // Sanity check:
18+ // Math.expm1(x) stays reasonably close to Math.exp(x) - 1 for large values.
19+ for ( var x = 1 ; x < 700 ; x += 0.25 ) {
1720 var expected = Math . exp ( x ) - 1 ;
18- assertEqualsDelta ( expected , Math . expm1 ( x ) , expected * 1E-14 ) ;
21+ assertEqualsDelta ( expected , Math . expm1 ( x ) , expected * 1E-15 ) ;
1922 expected = Math . exp ( - x ) - 1 ;
20- assertEqualsDelta ( expected , Math . expm1 ( - x ) , - expected * 1E-14 ) ;
23+ assertEqualsDelta ( expected , Math . expm1 ( - x ) , - expected * 1E-15 ) ;
2124}
2225
23- // Values close to 0:
26+ // Approximation for values close to 0:
2427// Use six terms of Taylor expansion at 0 for exp(x) as test expectation:
2528// exp(x) - 1 == exp(0) + exp(0) * x + x * x / 2 + ... - 1
2629// == x + x * x / 2 + x * x * x / 6 + ...
@@ -32,7 +35,44 @@ function expm1(x) {
3235 1 / 362880 + x * ( 1 / 3628800 ) ) ) ) ) ) ) ) ) ) ;
3336}
3437
38+ // Sanity check:
39+ // Math.expm1(x) stays reasonabliy close to the Taylor series for small values.
3540for ( var x = 1E-1 ; x > 1E-300 ; x *= 0.8 ) {
3641 var expected = expm1 ( x ) ;
37- assertEqualsDelta ( expected , Math . expm1 ( x ) , expected * 1E-14 ) ;
42+ assertEqualsDelta ( expected , Math . expm1 ( x ) , expected * 1E-15 ) ;
3843}
44+
45+
46+ // Tests related to the fdlibm implementation.
47+ // Test overflow.
48+ assertEquals ( Infinity , Math . expm1 ( 709.8 ) ) ;
49+ // Test largest double value.
50+ assertEquals ( Infinity , Math . exp ( 1.7976931348623157e308 ) ) ;
51+ // Cover various code paths.
52+ assertEquals ( - 1 , Math . expm1 ( - 56 * Math . LN2 ) ) ;
53+ assertEquals ( - 1 , Math . expm1 ( - 50 ) ) ;
54+ // Test most negative double value.
55+ assertEquals ( - 1 , Math . expm1 ( - 1.7976931348623157e308 ) ) ;
56+ // Test argument reduction.
57+ // Cases for 0.5*log(2) < |x| < 1.5*log(2).
58+ assertEquals ( Math . E - 1 , Math . expm1 ( 1 ) ) ;
59+ assertEquals ( 1 / Math . E - 1 , Math . expm1 ( - 1 ) ) ;
60+ // Cases for 1.5*log(2) < |x|.
61+ assertEquals ( 6.38905609893065 , Math . expm1 ( 2 ) ) ;
62+ assertEquals ( - 0.8646647167633873 , Math . expm1 ( - 2 ) ) ;
63+ // Cases where Math.expm1(x) = x.
64+ assertEquals ( 0 , Math . expm1 ( 0 ) ) ;
65+ assertEquals ( Math . pow ( 2 , - 55 ) , Math . expm1 ( Math . pow ( 2 , - 55 ) ) ) ;
66+ // Tests for the case where argument reduction has x in the primary range.
67+ // Test branch for k = 0.
68+ assertEquals ( 0.18920711500272105 , Math . expm1 ( 0.25 * Math . LN2 ) ) ;
69+ // Test branch for k = -1.
70+ assertEquals ( - 0.5 , Math . expm1 ( - Math . LN2 ) ) ;
71+ // Test branch for k = 1.
72+ assertEquals ( 1 , Math . expm1 ( Math . LN2 ) ) ;
73+ // Test branch for k <= -2 || k > 56. k = -3.
74+ assertEquals ( 1.4411518807585582e17 , Math . expm1 ( 57 * Math . LN2 ) ) ;
75+ // Test last branch for k < 20, k = 19.
76+ assertEquals ( 524286.99999999994 , Math . expm1 ( 19 * Math . LN2 ) ) ;
77+ // Test the else branch, k = 20.
78+ assertEquals ( 1048575 , Math . expm1 ( 20 * Math . LN2 ) ) ;
0 commit comments