2008-03-28 14:56:47 -06:00
|
|
|
// 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.
|
|
|
|
|
2008-06-27 18:06:23 -06:00
|
|
|
package math
|
2008-03-28 14:56:47 -06:00
|
|
|
|
|
|
|
/*
|
2008-11-19 17:14:31 -07:00
|
|
|
* Log returns the natural logarithm of its floating
|
2008-07-08 21:48:41 -06:00
|
|
|
* point argument.
|
|
|
|
*
|
|
|
|
* The coefficients are #2705 from Hart & Cheney. (19.38D)
|
|
|
|
*
|
|
|
|
* It calls frexp.
|
|
|
|
*/
|
2008-03-28 14:56:47 -06:00
|
|
|
|
|
|
|
const
|
|
|
|
(
|
|
|
|
log2 = .693147180559945309e0;
|
2008-11-19 17:14:31 -07:00
|
|
|
ln10u1 = .4342944819032518276511;
|
2008-03-28 14:56:47 -06:00
|
|
|
sqrto2 = .707106781186547524e0;
|
|
|
|
p0 = -.240139179559210510e2;
|
|
|
|
p1 = .309572928215376501e2;
|
|
|
|
p2 = -.963769093377840513e1;
|
|
|
|
p3 = .421087371217979714e0;
|
|
|
|
q0 = -.120069589779605255e2;
|
|
|
|
q1 = .194809660700889731e2;
|
|
|
|
q2 = -.891110902798312337e1;
|
|
|
|
)
|
|
|
|
|
2008-11-19 17:14:31 -07:00
|
|
|
export func Log(arg float64) float64 {
|
2008-03-28 14:56:47 -06:00
|
|
|
if arg <= 0 {
|
2008-07-07 15:07:46 -06:00
|
|
|
return sys.NaN();
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
|
2008-11-19 17:14:31 -07:00
|
|
|
x, exp := sys.frexp(arg);
|
2008-03-28 14:56:47 -06:00
|
|
|
for x < 0.5 {
|
|
|
|
x = x*2;
|
|
|
|
exp = exp-1;
|
|
|
|
}
|
|
|
|
if x < sqrto2 {
|
|
|
|
x = x*2;
|
|
|
|
exp = exp-1;
|
|
|
|
}
|
|
|
|
|
2008-11-19 17:14:31 -07:00
|
|
|
z := (x-1) / (x+1);
|
|
|
|
zsq := z*z;
|
2008-03-28 14:56:47 -06:00
|
|
|
|
2008-11-19 17:14:31 -07:00
|
|
|
temp := ((p3*zsq + p2)*zsq + p1)*zsq + p0;
|
2008-03-28 14:56:47 -06:00
|
|
|
temp = temp/(((zsq + q2)*zsq + q1)*zsq + q0);
|
2008-07-08 21:48:41 -06:00
|
|
|
temp = temp*z + float64(exp)*log2;
|
2008-03-28 14:56:47 -06:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2008-11-19 17:14:31 -07:00
|
|
|
export func Log10(arg float64) float64 {
|
2008-03-28 14:56:47 -06:00
|
|
|
if arg <= 0 {
|
2008-07-07 15:07:46 -06:00
|
|
|
return sys.NaN();
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2008-11-19 17:14:31 -07:00
|
|
|
return Log(arg) * ln10u1;
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|