]> Cypherpunks repositories - gostls13.git/commitdiff
math: fix inaccurate result of Exp(1)
authorcrvv <crvv.mail@gmail.com>
Tue, 18 Jul 2017 06:37:01 +0000 (14:37 +0800)
committerRobert Griesemer <gri@golang.org>
Thu, 17 Aug 2017 09:01:27 +0000 (09:01 +0000)
The existing implementation is translated from C, which uses a
polynomial coefficient very close to 1/6. If the function uses
1/6 as this coeffient, the result of Exp(1) will be more accurate.
And this change doesn't introduce more error to Exp function.

Fixes #20319

Change-Id: I94c236a18cf95570ebb69f7fb99884b0d7cf5f6e
Reviewed-on: https://go-review.googlesource.com/49294
Reviewed-by: Robert Griesemer <gri@golang.org>
src/math/all_test.go
src/math/exp.go

index bdc4d228d59ae54af58cbd82517e3a3809f2c175..11bb8b2564876d82cb6ff331b2ea8ee457b3b604 100644 (file)
@@ -953,6 +953,7 @@ var vfexpSC = []float64{
        // Issue 18912
        1.48852223e+09,
        1.4885222e+09,
+       1,
 }
 var expSC = []float64{
        0,
@@ -963,6 +964,7 @@ var expSC = []float64{
        Inf(1),
        Inf(1),
        Inf(1),
+       2.718281828459045,
 }
 
 var vfexp2SC = []float64{
index 3268c9888cace2a07f64b2281efa23663840179d..bd4c5c9b7144bb30eca0a0f26f47cc884f2c8b36 100644 (file)
@@ -44,7 +44,7 @@ func Exp(x float64) float64
 //      the interval [0,0.34658]:
 //      Write
 //          R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
-//      We use a special Remes algorithm on [0,0.34658] to generate
+//      We use a special Remez algorithm on [0,0.34658] to generate
 //      a polynomial of degree 5 to approximate R. The maximum error
 //      of this polynomial approximation is bounded by 2**-59. In
 //      other words,
@@ -175,7 +175,7 @@ func exp2(x float64) float64 {
 // exp1 returns e**r × 2**k where r = hi - lo and |r| ≤ ln(2)/2.
 func expmulti(hi, lo float64, k int) float64 {
        const (
-               P1 = 1.66666666666666019037e-01  /* 0x3FC55555; 0x5555553E */
+               P1 = 1.66666666666666657415e-01  /* 0x3FC55555; 0x55555555 */
                P2 = -2.77777777770155933842e-03 /* 0xBF66C16C; 0x16BEBD93 */
                P3 = 6.61375632143793436117e-05  /* 0x3F11566A; 0xAF25DE2C */
                P4 = -1.65339022054652515390e-06 /* 0xBEBBBD41; 0xC5D26BF1 */