60
60
class FdLibm {
61
61
// Constants used by multiple algorithms
62
62
private static final double INFINITY = Double .POSITIVE_INFINITY ;
63
+ private static final double TWO54 = 0x1.0p54 ; // 1.80143985094819840000e+16
63
64
64
65
private FdLibm () {
65
66
throw new UnsupportedOperationException ("No FdLibm instances for you." );
@@ -779,11 +780,10 @@ public static double compute(double x) {
779
780
* shown.
780
781
*/
781
782
static class Log10 {
782
- private static double two54 = 0x1.0p54 ; // 1.80143985094819840000e+16;
783
- private static double ivln10 = 0x1.bcb7b1526e50ep-2 ; // 4.34294481903251816668e-01
783
+ private static final double ivln10 = 0x1.bcb7b1526e50ep-2 ; // 4.34294481903251816668e-01
784
784
785
- private static double log10_2hi = 0x1.34413509f6p-2 ; // 3.01029995663611771306e-01;
786
- private static double log10_2lo = 0x1.9fef311f12b36p-42 ; // 3.69423907715893078616e-13;
785
+ private static final double log10_2hi = 0x1.34413509f6p-2 ; // 3.01029995663611771306e-01;
786
+ private static final double log10_2lo = 0x1.9fef311f12b36p-42 ; // 3.69423907715893078616e-13;
787
787
788
788
private Log10 () {
789
789
throw new UnsupportedOperationException ();
@@ -799,13 +799,13 @@ public static double compute(double x) {
799
799
k =0 ;
800
800
if (hx < 0x0010_0000 ) { /* x < 2**-1022 */
801
801
if (((hx & 0x7fff_ffff ) | lx ) == 0 ) {
802
- return -two54 /0.0 ; /* log(+-0)=-inf */
802
+ return -TWO54 /0.0 ; /* log(+-0)=-inf */
803
803
}
804
804
if (hx < 0 ) {
805
805
return (x - x )/0.0 ; /* log(-#) = NaN */
806
806
}
807
807
k -= 54 ;
808
- x *= two54 ; /* subnormal number, scale up x */
808
+ x *= TWO54 ; /* subnormal number, scale up x */
809
809
hx = __HI (x );
810
810
}
811
811
@@ -822,4 +822,167 @@ public static double compute(double x) {
822
822
return z + y * log10_2hi ;
823
823
}
824
824
}
825
+
826
+ /**
827
+ * Returns the natural logarithm of the sum of the argument and 1.
828
+ *
829
+ * Method :
830
+ * 1. Argument Reduction: find k and f such that
831
+ * 1+x = 2^k * (1+f),
832
+ * where sqrt(2)/2 < 1+f < sqrt(2) .
833
+ *
834
+ * Note. If k=0, then f=x is exact. However, if k!=0, then f
835
+ * may not be representable exactly. In that case, a correction
836
+ * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
837
+ * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
838
+ * and add back the correction term c/u.
839
+ * (Note: when x > 2**53, one can simply return log(x))
840
+ *
841
+ * 2. Approximation of log1p(f).
842
+ * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
843
+ * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
844
+ * = 2s + s*R
845
+ * We use a special Reme algorithm on [0,0.1716] to generate
846
+ * a polynomial of degree 14 to approximate R The maximum error
847
+ * of this polynomial approximation is bounded by 2**-58.45. In
848
+ * other words,
849
+ * 2 4 6 8 10 12 14
850
+ * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
851
+ * (the values of Lp1 to Lp7 are listed in the program)
852
+ * and
853
+ * | 2 14 | -58.45
854
+ * | Lp1*s +...+Lp7*s - R(z) | <= 2
855
+ * | |
856
+ * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
857
+ * In order to guarantee error in log below 1ulp, we compute log
858
+ * by
859
+ * log1p(f) = f - (hfsq - s*(hfsq+R)).
860
+ *
861
+ * 3. Finally, log1p(x) = k*ln2 + log1p(f).
862
+ * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
863
+ * Here ln2 is split into two floating point number:
864
+ * ln2_hi + ln2_lo,
865
+ * where n*ln2_hi is always exact for |n| < 2000.
866
+ *
867
+ * Special cases:
868
+ * log1p(x) is NaN with signal if x < -1 (including -INF) ;
869
+ * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
870
+ * log1p(NaN) is that NaN with no signal.
871
+ *
872
+ * Accuracy:
873
+ * according to an error analysis, the error is always less than
874
+ * 1 ulp (unit in the last place).
875
+ *
876
+ * Constants:
877
+ * The hexadecimal values are the intended ones for the following
878
+ * constants. The decimal values may be used, provided that the
879
+ * compiler will convert from decimal to binary accurately enough
880
+ * to produce the hexadecimal values shown.
881
+ *
882
+ * Note: Assuming log() return accurate answer, the following
883
+ * algorithm can be used to compute log1p(x) to within a few ULP:
884
+ *
885
+ * u = 1+x;
886
+ * if(u==1.0) return x ; else
887
+ * return log(u)*(x/(u-1.0));
888
+ *
889
+ * See HP-15C Advanced Functions Handbook, p.193.
890
+ */
891
+ static class Log1p {
892
+ private static final double ln2_hi = 0x1.62e42feep-1 ; // 6.93147180369123816490e-01
893
+ private static final double ln2_lo = 0x1.a39ef35793c76p-33 ; // 1.90821492927058770002e-10
894
+ private static final double Lp1 = 0x1.5555555555593p-1 ; // 6.666666666666735130e-01
895
+ private static final double Lp2 = 0x1.999999997fa04p-2 ; // 3.999999999940941908e-01
896
+ private static final double Lp3 = 0x1.2492494229359p-2 ; // 2.857142874366239149e-01
897
+ private static final double Lp4 = 0x1.c71c51d8e78afp-3 ; // 2.222219843214978396e-01
898
+ private static final double Lp5 = 0x1.7466496cb03dep-3 ; // 1.818357216161805012e-01
899
+ private static final double Lp6 = 0x1.39a09d078c69fp-3 ; // 1.531383769920937332e-01
900
+ private static final double Lp7 = 0x1.2f112df3e5244p-3 ; // 1.479819860511658591e-01
901
+
902
+ public static double compute (double x ) {
903
+ double hfsq , f =0 , c =0 , s , z , R , u ;
904
+ int k , hx , hu =0 , ax ;
905
+
906
+ hx = __HI (x ); /* high word of x */
907
+ ax = hx & 0x7fff_ffff ;
908
+
909
+ k = 1 ;
910
+ if (hx < 0x3FDA_827A ) { /* x < 0.41422 */
911
+ if (ax >= 0x3ff0_0000 ) { /* x <= -1.0 */
912
+ if (x == -1.0 ) /* log1p(-1)=-inf */
913
+ return -INFINITY ;
914
+ else
915
+ return Double .NaN ; /* log1p(x < -1) = NaN */
916
+ }
917
+
918
+ if (ax < 0x3e20_0000 ) { /* |x| < 2**-29 */
919
+ if (TWO54 + x > 0.0 /* raise inexact */
920
+ && ax < 0x3c90_0000 ) /* |x| < 2**-54 */
921
+ return x ;
922
+ else
923
+ return x - x *x *0.5 ;
924
+ }
925
+
926
+ if (hx > 0 || hx <= 0xbfd2_bec3 ) { /* -0.2929 < x < 0.41422 */
927
+ k =0 ;
928
+ f =x ;
929
+ hu =1 ;
930
+ }
931
+ }
932
+
933
+ if (hx >= 0x7ff0_0000 ) {
934
+ return x + x ;
935
+ }
936
+
937
+ if (k != 0 ) {
938
+ if (hx < 0x4340_0000 ) {
939
+ u = 1.0 + x ;
940
+ hu = __HI (u ); /* high word of u */
941
+ k = (hu >> 20 ) - 1023 ;
942
+ c = (k > 0 )? 1.0 - (u -x ) : x -(u -1.0 ); /* correction term */
943
+ c /= u ;
944
+ } else {
945
+ u = x ;
946
+ hu = __HI (u ); /* high word of u */
947
+ k = (hu >> 20 ) - 1023 ;
948
+ c = 0 ;
949
+ }
950
+ hu &= 0x000f_ffff ;
951
+ if (hu < 0x6_a09e ) {
952
+ u = __HI (u , hu | 0x3ff0_0000 ); /* normalize u */
953
+ } else {
954
+ k += 1 ;
955
+ u = __HI (u , hu | 0x3fe0_0000 ); /* normalize u/2 */
956
+ hu = (0x0010_0000 - hu ) >> 2 ;
957
+ }
958
+ f = u - 1.0 ;
959
+ }
960
+
961
+ hfsq = 0.5 *f *f ;
962
+ if (hu == 0 ) { /* |f| < 2**-20 */
963
+ if (f == 0.0 ) {
964
+ if (k == 0 ) {
965
+ return 0.0 ;
966
+ } else {
967
+ c += k * ln2_lo ;
968
+ return k * ln2_hi + c ;
969
+ }
970
+ }
971
+ R = hfsq * (1.0 - 0.66666666666666666 *f );
972
+ if (k == 0 ) {
973
+ return f - R ;
974
+ } else {
975
+ return k * ln2_hi - ((R -(k * ln2_lo +c )) - f );
976
+ }
977
+ }
978
+ s = f /(2.0 + f );
979
+ z = s * s ;
980
+ R = z * (Lp1 + z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z *Lp7 ))))));
981
+ if (k == 0 ) {
982
+ return f - (hfsq - s *(hfsq + R ));
983
+ } else {
984
+ return k * ln2_hi - ((hfsq - (s *(hfsq + R ) + (k * ln2_lo +c ))) - f );
985
+ }
986
+ }
987
+ }
825
988
}
0 commit comments