diff options
Diffstat (limited to 'src/lib/math')
-rw-r--r-- | src/lib/math/Makefile | 11 | ||||
-rw-r--r-- | src/lib/math/atan.go | 50 | ||||
-rw-r--r-- | src/lib/math/const.go | 9 | ||||
-rw-r--r-- | src/lib/math/log.go | 37 | ||||
-rw-r--r-- | src/lib/math/sin.go | 36 | ||||
-rw-r--r-- | src/lib/math/sinh.go | 18 |
6 files changed, 84 insertions, 77 deletions
diff --git a/src/lib/math/Makefile b/src/lib/math/Makefile index 672d17fac..ef31b174a 100644 --- a/src/lib/math/Makefile +++ b/src/lib/math/Makefile @@ -38,33 +38,34 @@ O1=\ floor.$O\ fmod.$O\ hypot.$O\ - log.$O\ pow10.$O\ sin.$O\ sqrt.$O\ tan.$O\ + const.$O\ O2=\ asin.$O\ atan2.$O\ - pow.$O\ + log.$O\ sinh.$O\ O3=\ + pow.$O\ tanh.$O\ math.a: a1 a2 a3 a1: $(O1) - $(AR) grc math.a atan.$O exp.$O fabs.$O floor.$O fmod.$O hypot.$O log.$O pow10.$O sin.$O sqrt.$O tan.$O + $(AR) grc math.a atan.$O exp.$O fabs.$O floor.$O fmod.$O hypot.$O pow10.$O sin.$O sqrt.$O tan.$O const.$O rm -f $(O1) a2: $(O2) - $(AR) grc math.a asin.$O atan2.$O pow.$O sinh.$O + $(AR) grc math.a asin.$O atan2.$O log.$O sinh.$O rm -f $(O2) a3: $(O3) - $(AR) grc math.a tanh.$O + $(AR) grc math.a pow.$O tanh.$O rm -f $(O3) newpkg: clean diff --git a/src/lib/math/atan.go b/src/lib/math/atan.go index 43990fe40..f2fd7ed33 100644 --- a/src/lib/math/atan.go +++ b/src/lib/math/atan.go @@ -15,30 +15,30 @@ package math const ( - p4 = .161536412982230228262e2; - p3 = .26842548195503973794141e3; - p2 = .11530293515404850115428136e4; - p1 = .178040631643319697105464587e4; - p0 = .89678597403663861959987488e3; - q4 = .5895697050844462222791e2; - q3 = .536265374031215315104235e3; - q2 = .16667838148816337184521798e4; - q1 = .207933497444540981287275926e4; - q0 = .89678597403663861962481162e3; - pio2 = .15707963267948966192313216e1; - pio4 = .7853981633974483096156608e0; - sq2p1 = .2414213562373095048802e1; // sqrt(2)+1 - sq2m1 = .414213562373095048802e0; // sqrt(2)-1 + ap4 = .161536412982230228262e2; + ap3 = .26842548195503973794141e3; + ap2 = .11530293515404850115428136e4; + ap1 = .178040631643319697105464587e4; + ap0 = .89678597403663861959987488e3; + aq4 = .5895697050844462222791e2; + aq3 = .536265374031215315104235e3; + aq2 = .16667838148816337184521798e4; + aq1 = .207933497444540981287275926e4; + aq0 = .89678597403663861962481162e3; + apio2 = .15707963267948966192313216e1; + apio4 = .7853981633974483096156608e0; + asq2p1 = .2414213562373095048802e1; // sqrt(2)+1 + asq2m1 = .414213562373095048802e0; // sqrt(2)-1 ) /* * xatan evaluates a series valid in the * range [-0.414...,+0.414...]. (tan(pi/8)) */ -func Xatan(arg float64) float64 { +func xatan(arg float64) float64 { argsq := arg*arg; - value := ((((p4*argsq + p3)*argsq + p2)*argsq + p1)*argsq + p0); - value = value/(((((argsq + q4)*argsq + q3)*argsq + q2)*argsq + q1)*argsq + q0); + value := ((((ap4*argsq + ap3)*argsq + ap2)*argsq + ap1)*argsq + ap0); + value = value/(((((argsq + aq4)*argsq + aq3)*argsq + aq2)*argsq + aq1)*argsq + aq0); return value*arg; } @@ -46,14 +46,14 @@ func Xatan(arg float64) float64 { * satan reduces its argument (known to be positive) * to the range [0,0.414...] and calls xatan. */ -func Satan(arg float64) float64 { - if arg < sq2m1 { - return Xatan(arg); +func satan(arg float64) float64 { + if arg < asq2m1 { + return xatan(arg); } - if arg > sq2p1 { - return pio2 - Xatan(1/arg); + if arg > asq2p1 { + return apio2 - xatan(1/arg); } - return pio4 + Xatan((arg-1)/(arg+1)); + return apio4 + xatan((arg-1)/(arg+1)); } /* @@ -62,7 +62,7 @@ func Satan(arg float64) float64 { */ export func Atan(arg float64) float64 { if arg > 0 { - return Satan(arg); + return satan(arg); } - return -Satan(-arg); + return -satan(-arg); } diff --git a/src/lib/math/const.go b/src/lib/math/const.go new file mode 100644 index 000000000..a1c5f8e1a --- /dev/null +++ b/src/lib/math/const.go @@ -0,0 +1,9 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package math + +export const ( + Sqrt2 = 1.41421356237309504880168872420969808; +) diff --git a/src/lib/math/log.go b/src/lib/math/log.go index c0cfebf8b..9c54858b4 100644 --- a/src/lib/math/log.go +++ b/src/lib/math/log.go @@ -4,6 +4,8 @@ package math +import "math" + // The original C code, the long comment, and the constants // below are from FreeBSD's /usr/src/lib/msun/src/e_log.c // and came with this notice. The go code is a simpler @@ -35,11 +37,11 @@ package math // of this polynomial approximation is bounded by 2**-58.45. In // other words, // 2 4 6 8 10 12 14 -// R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s -// (the values of Lg1 to Lg7 are listed in the program) +// R(z) ~ lg1*s +lg2*s +lg3*s +lg4*s +lg5*s +lg6*s +lg7*s +// (the values of lg1 to lg7 are listed in the program) // and // | 2 14 | -58.45 -// | Lg1*s +...+Lg7*s - R(z) | <= 2 +// | lg1*s +...+lg7*s - R(z) | <= 2 // | | // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. // In order to guarantee error in log below 1ulp, we compute log @@ -69,20 +71,15 @@ package math // to produce the hexadecimal values shown. const ( - Ln2Hi = 6.93147180369123816490e-01; /* 3fe62e42 fee00000 */ - Ln2Lo = 1.90821492927058770002e-10; /* 3dea39ef 35793c76 */ - Lg1 = 6.666666666666735130e-01; /* 3FE55555 55555593 */ - Lg2 = 3.999999999940941908e-01; /* 3FD99999 9997FA04 */ - Lg3 = 2.857142874366239149e-01; /* 3FD24924 94229359 */ - Lg4 = 2.222219843214978396e-01; /* 3FCC71C5 1D8E78AF */ - Lg5 = 1.818357216161805012e-01; /* 3FC74664 96CB03DE */ - Lg6 = 1.531383769920937332e-01; /* 3FC39A09 D078C69F */ - Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ - - Two54 = 1<<54; // 2^54 - TwoM20 = 1.0/(1<<20); // 2^-20 - TwoM1022 = 2.2250738585072014e-308; // 2^-1022 - Sqrt2 = 1.41421356237309504880168872420969808; + ln2Hi = 6.93147180369123816490e-01; /* 3fe62e42 fee00000 */ + ln2Lo = 1.90821492927058770002e-10; /* 3dea39ef 35793c76 */ + lg1 = 6.666666666666735130e-01; /* 3FE55555 55555593 */ + lg2 = 3.999999999940941908e-01; /* 3FD99999 9997FA04 */ + lg3 = 2.857142874366239149e-01; /* 3FD24924 94229359 */ + lg4 = 2.222219843214978396e-01; /* 3FCC71C5 1D8E78AF */ + lg5 = 1.818357216161805012e-01; /* 3FC74664 96CB03DE */ + lg6 = 1.531383769920937332e-01; /* 3FC39A09 D078C69F */ + lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ ) export func Log(x float64) float64 { @@ -109,11 +106,11 @@ export func Log(x float64) float64 { s := f/(2+f); s2 := s*s; s4 := s2*s2; - t1 := s2*(Lg1 + s4*(Lg3 + s4*(Lg5 + s4*Lg7))); - t2 := s4*(Lg2 + s4*(Lg4 + s4*Lg6)); + t1 := s2*(lg1 + s4*(lg3 + s4*(lg5 + s4*lg7))); + t2 := s4*(lg2 + s4*(lg4 + s4*lg6)); R := t1 + t2; hfsq := 0.5*f*f; - return k*Ln2Hi - ((hfsq-(s*(hfsq+R)+k*Ln2Lo)) - f); + return k*ln2Hi - ((hfsq-(s*(hfsq+R)+k*ln2Lo)) - f); } const diff --git a/src/lib/math/sin.go b/src/lib/math/sin.go index 57de55913..077506b30 100644 --- a/src/lib/math/sin.go +++ b/src/lib/math/sin.go @@ -9,32 +9,32 @@ package math */ const ( - p0 = .1357884097877375669092680e8; - p1 = -.4942908100902844161158627e7; - p2 = .4401030535375266501944918e6; - p3 = -.1384727249982452873054457e5; - p4 = .1459688406665768722226959e3; - q0 = .8644558652922534429915149e7; - q1 = .4081792252343299749395779e6; - q2 = .9463096101538208180571257e4; - q3 = .1326534908786136358911494e3; + sp0 = .1357884097877375669092680e8; + sp1 = -.4942908100902844161158627e7; + sp2 = .4401030535375266501944918e6; + sp3 = -.1384727249982452873054457e5; + sp4 = .1459688406665768722226959e3; + sq0 = .8644558652922534429915149e7; + sq1 = .4081792252343299749395779e6; + sq2 = .9463096101538208180571257e4; + sq3 = .1326534908786136358911494e3; - piu2 = .6366197723675813430755350e0; // 2/pi + spiu2 = .6366197723675813430755350e0; // 2/pi ) -func Sinus(arg float64, quad int) float64 { +func sinus(arg float64, quad int) float64 { x := arg; if(x < 0) { x = -x; quad = quad+2; } - x = x * piu2; /* underflow? */ + x = x * spiu2; /* underflow? */ var y float64; if x > 32764 { var e float64; e, y = sys.modf(x); e = e + float64(quad); - temp1, f := sys.modf(0.25*e); + temsp1, f := sys.modf(0.25*e); quad = int(e - 4*f); } else { k := int32(x); @@ -50,18 +50,18 @@ func Sinus(arg float64, quad int) float64 { } ysq := y*y; - temp1 := ((((p4*ysq+p3)*ysq+p2)*ysq+p1)*ysq+p0)*y; - temp2 := ((((ysq+q3)*ysq+q2)*ysq+q1)*ysq+q0); - return temp1/temp2; + temsp1 := ((((sp4*ysq+sp3)*ysq+sp2)*ysq+sp1)*ysq+sp0)*y; + temsp2 := ((((ysq+sq3)*ysq+sq2)*ysq+sq1)*ysq+sq0); + return temsp1/temsp2; } export func Cos(arg float64) float64 { if arg < 0 { arg = -arg; } - return Sinus(arg, 1); + return sinus(arg, 1); } export func Sin(arg float64) float64 { - return Sinus(arg, 0); + return sinus(arg, 0); } diff --git a/src/lib/math/sinh.go b/src/lib/math/sinh.go index e1fde0ac2..622467ed7 100644 --- a/src/lib/math/sinh.go +++ b/src/lib/math/sinh.go @@ -22,13 +22,13 @@ import "math" const ( - p0 = -0.6307673640497716991184787251e+6; - p1 = -0.8991272022039509355398013511e+5; - p2 = -0.2894211355989563807284660366e+4; - p3 = -0.2630563213397497062819489e+2; - q0 = -0.6307673640497716991212077277e+6; - q1 = 0.1521517378790019070696485176e+5; - q2 = -0.173678953558233699533450911e+3; + shp0 = -0.6307673640497716991184787251e+6; + shp1 = -0.8991272022039509355398013511e+5; + shp2 = -0.2894211355989563807284660366e+4; + shp3 = -0.2630563213397497062819489e+2; + shq0 = -0.6307673640497716991212077277e+6; + shq1 = 0.1521517378790019070696485176e+5; + shq2 = -0.173678953558233699533450911e+3; ) export func Sinh(arg float64) float64 { @@ -48,8 +48,8 @@ export func Sinh(arg float64) float64 { default: argsq := arg*arg; - temp = (((p3*argsq+p2)*argsq+p1)*argsq+p0)*arg; - temp = temp/(((argsq+q2)*argsq+q1)*argsq+q0); + temp = (((shp3*argsq+shp2)*argsq+shp1)*argsq+shp0)*arg; + temp = temp/(((argsq+shq2)*argsq+shq1)*argsq+shq0); } if sign { |