@@ -985,4 +985,213 @@ public static double compute(double x) {
985
985
}
986
986
}
987
987
}
988
+
989
+ /* expm1(x)
990
+ * Returns exp(x)-1, the exponential of x minus 1.
991
+ *
992
+ * Method
993
+ * 1. Argument reduction:
994
+ * Given x, find r and integer k such that
995
+ *
996
+ * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
997
+ *
998
+ * Here a correction term c will be computed to compensate
999
+ * the error in r when rounded to a floating-point number.
1000
+ *
1001
+ * 2. Approximating expm1(r) by a special rational function on
1002
+ * the interval [0,0.34658]:
1003
+ * Since
1004
+ * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
1005
+ * we define R1(r*r) by
1006
+ * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
1007
+ * That is,
1008
+ * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
1009
+ * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
1010
+ * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
1011
+ * We use a special Reme algorithm on [0,0.347] to generate
1012
+ * a polynomial of degree 5 in r*r to approximate R1. The
1013
+ * maximum error of this polynomial approximation is bounded
1014
+ * by 2**-61. In other words,
1015
+ * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
1016
+ * where Q1 = -1.6666666666666567384E-2,
1017
+ * Q2 = 3.9682539681370365873E-4,
1018
+ * Q3 = -9.9206344733435987357E-6,
1019
+ * Q4 = 2.5051361420808517002E-7,
1020
+ * Q5 = -6.2843505682382617102E-9;
1021
+ * (where z=r*r, and the values of Q1 to Q5 are listed below)
1022
+ * with error bounded by
1023
+ * | 5 | -61
1024
+ * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
1025
+ * | |
1026
+ *
1027
+ * expm1(r) = exp(r)-1 is then computed by the following
1028
+ * specific way which minimize the accumulation rounding error:
1029
+ * 2 3
1030
+ * r r [ 3 - (R1 + R1*r/2) ]
1031
+ * expm1(r) = r + --- + --- * [--------------------]
1032
+ * 2 2 [ 6 - r*(3 - R1*r/2) ]
1033
+ *
1034
+ * To compensate the error in the argument reduction, we use
1035
+ * expm1(r+c) = expm1(r) + c + expm1(r)*c
1036
+ * ~ expm1(r) + c + r*c
1037
+ * Thus c+r*c will be added in as the correction terms for
1038
+ * expm1(r+c). Now rearrange the term to avoid optimization
1039
+ * screw up:
1040
+ * ( 2 2 )
1041
+ * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
1042
+ * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
1043
+ * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
1044
+ * ( )
1045
+ *
1046
+ * = r - E
1047
+ * 3. Scale back to obtain expm1(x):
1048
+ * From step 1, we have
1049
+ * expm1(x) = either 2^k*[expm1(r)+1] - 1
1050
+ * = or 2^k*[expm1(r) + (1-2^-k)]
1051
+ * 4. Implementation notes:
1052
+ * (A). To save one multiplication, we scale the coefficient Qi
1053
+ * to Qi*2^i, and replace z by (x^2)/2.
1054
+ * (B). To achieve maximum accuracy, we compute expm1(x) by
1055
+ * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
1056
+ * (ii) if k=0, return r-E
1057
+ * (iii) if k=-1, return 0.5*(r-E)-0.5
1058
+ * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
1059
+ * else return 1.0+2.0*(r-E);
1060
+ * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
1061
+ * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
1062
+ * (vii) return 2^k(1-((E+2^-k)-r))
1063
+ *
1064
+ * Special cases:
1065
+ * expm1(INF) is INF, expm1(NaN) is NaN;
1066
+ * expm1(-INF) is -1, and
1067
+ * for finite argument, only expm1(0)=0 is exact.
1068
+ *
1069
+ * Accuracy:
1070
+ * according to an error analysis, the error is always less than
1071
+ * 1 ulp (unit in the last place).
1072
+ *
1073
+ * Misc. info.
1074
+ * For IEEE double
1075
+ * if x > 7.09782712893383973096e+02 then expm1(x) overflow
1076
+ *
1077
+ * Constants:
1078
+ * The hexadecimal values are the intended ones for the following
1079
+ * constants. The decimal values may be used, provided that the
1080
+ * compiler will convert from decimal to binary accurately enough
1081
+ * to produce the hexadecimal values shown.
1082
+ */
1083
+ static class Expm1 {
1084
+ private static final double one = 1.0 ;
1085
+ private static final double huge = 1.0e+300 ;
1086
+ private static final double tiny = 1.0e-300 ;
1087
+ private static final double o_threshold = 0x1.62e42fefa39efp9 ; // 7.09782712893383973096e+02
1088
+ private static final double ln2_hi = 0x1.62e42feep-1 ; // 6.93147180369123816490e-01
1089
+ private static final double ln2_lo = 0x1.a39ef35793c76p-33 ; // 1.90821492927058770002e-10
1090
+ private static final double invln2 = 0x1.71547652b82fep0 ; // 1.44269504088896338700e+00
1091
+ // scaled coefficients related to expm1
1092
+ private static final double Q1 = -0x1.11111111110f4p-5 ; // -3.33333333333331316428e-02
1093
+ private static final double Q2 = 0x1.a01a019fe5585p-10 ; // 1.58730158725481460165e-03
1094
+ private static final double Q3 = -0x1.4ce199eaadbb7p-14 ; // -7.93650757867487942473e-05
1095
+ private static final double Q4 = 0x1.0cfca86e65239p-18 ; // 4.00821782732936239552e-06
1096
+ private static final double Q5 = -0x1.afdb76e09c32dp-23 ; // -2.01099218183624371326e-07
1097
+
1098
+ static double compute (double x ) {
1099
+ double y , hi , lo , c =0 , t , e , hxs , hfx , r1 ;
1100
+ int k , xsb ;
1101
+ /*unsigned*/ int hx ;
1102
+
1103
+ hx = __HI (x ); // high word of x
1104
+ xsb = hx & 0x8000_0000 ; // sign bit of x
1105
+ y = Math .abs (x );
1106
+ hx &= 0x7fff_ffff ; // high word of |x|
1107
+
1108
+ // filter out huge and non-finite argument
1109
+ if (hx >= 0x4043_687A ) { // if |x| >= 56*ln2
1110
+ if (hx >= 0x4086_2E42 ) { // if |x| >= 709.78...
1111
+ if (hx >= 0x7ff_00000 ) {
1112
+ if (((hx & 0xf_ffff ) | __LO (x )) != 0 ) {
1113
+ return x + x ; // NaN
1114
+ } else {
1115
+ return (xsb == 0 )? x : -1.0 ; // exp(+-inf)={inf,-1}
1116
+ }
1117
+ }
1118
+ if (x > o_threshold ) {
1119
+ return huge *huge ; // overflow
1120
+ }
1121
+ }
1122
+ if (xsb != 0 ) { // x < -56*ln2, return -1.0 with inexact
1123
+ if (x + tiny < 0.0 ) { // raise inexact
1124
+ return tiny - one ; // return -1
1125
+ }
1126
+ }
1127
+ }
1128
+
1129
+ // argument reduction
1130
+ if (hx > 0x3fd6_2e42 ) { // if |x| > 0.5 ln2
1131
+ if (hx < 0x3FF0_A2B2 ) { // and |x| < 1.5 ln2
1132
+ if (xsb == 0 ) {
1133
+ hi = x - ln2_hi ;
1134
+ lo = ln2_lo ;
1135
+ k = 1 ;
1136
+ } else {
1137
+ hi = x + ln2_hi ;
1138
+ lo = -ln2_lo ;
1139
+ k = -1 ;
1140
+ }
1141
+ } else {
1142
+ k = (int )(invln2 *x + ((xsb == 0 ) ? 0.5 : -0.5 ));
1143
+ t = k ;
1144
+ hi = x - t *ln2_hi ; // t*ln2_hi is exact here
1145
+ lo = t *ln2_lo ;
1146
+ }
1147
+ x = hi - lo ;
1148
+ c = (hi - x ) - lo ;
1149
+ } else if (hx < 0x3c90_0000 ) { // when |x| < 2**-54, return x
1150
+ t = huge + x ; // return x with inexact flags when x != 0
1151
+ return x - (t - (huge + x ));
1152
+ } else {
1153
+ k = 0 ;
1154
+ }
1155
+
1156
+ // x is now in primary range
1157
+ hfx = 0.5 *x ;
1158
+ hxs = x *hfx ;
1159
+ r1 = one + hxs *(Q1 + hxs *(Q2 + hxs *(Q3 + hxs *(Q4 + hxs *Q5 ))));
1160
+ t = 3.0 - r1 *hfx ;
1161
+ e = hxs *((r1 - t )/(6.0 - x *t ));
1162
+ if (k == 0 ) {
1163
+ return x - (x *e - hxs ); // c is 0
1164
+ } else {
1165
+ e = (x *(e - c ) - c );
1166
+ e -= hxs ;
1167
+ if (k == -1 ) {
1168
+ return 0.5 *(x - e ) - 0.5 ;
1169
+ }
1170
+ if (k == 1 ) {
1171
+ if (x < -0.25 ) {
1172
+ return -2.0 *(e - (x + 0.5 ));
1173
+ } else {
1174
+ return one + 2.0 *(x - e );
1175
+ }
1176
+ }
1177
+ if (k <= -2 || k > 56 ) { // suffice to return exp(x) - 1
1178
+ y = one - (e - x );
1179
+ y = __HI (y , __HI (y ) + (k << 20 )); // add k to y's exponent
1180
+ return y - one ;
1181
+ }
1182
+ t = one ;
1183
+ if (k < 20 ) {
1184
+ t = __HI (t , 0x3ff0_0000 - (0x2_00000 >> k )); // t = 1-2^-k
1185
+ y = t - ( e - x );
1186
+ y = __HI (y , __HI (y ) + (k << 20 )); // add k to y's exponent
1187
+ } else {
1188
+ t = __HI (t , ((0x3ff - k ) << 20 )); // 2^-k
1189
+ y = x - (e + t );
1190
+ y += one ;
1191
+ y = __HI (y , __HI (y ) + (k << 20 )); // add k to y's exponent
1192
+ }
1193
+ }
1194
+ return y ;
1195
+ }
1196
+ }
988
1197
}
0 commit comments