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-07-08 21:48:41 -06:00
|
|
|
* floating-point arctangent
|
|
|
|
*
|
|
|
|
* atan returns the value of the arctangent of its
|
|
|
|
* argument in the range [-pi/2,pi/2].
|
|
|
|
* there are no error returns.
|
|
|
|
* coefficients are #5077 from Hart & Cheney. (19.56D)
|
2008-07-19 15:37:02 -06:00
|
|
|
*/
|
2008-03-28 14:56:47 -06:00
|
|
|
|
|
|
|
/*
|
2008-07-08 21:48:41 -06:00
|
|
|
* xatan evaluates a series valid in the
|
|
|
|
* range [-0.414...,+0.414...]. (tan(pi/8))
|
2008-03-28 14:56:47 -06:00
|
|
|
*/
|
2009-01-15 17:16:42 -07:00
|
|
|
func xatan(arg float64) float64 {
|
2009-10-06 20:40:35 -06:00
|
|
|
const (
|
2009-12-15 16:35:38 -07:00
|
|
|
P4 = .161536412982230228262e2
|
|
|
|
P3 = .26842548195503973794141e3
|
|
|
|
P2 = .11530293515404850115428136e4
|
|
|
|
P1 = .178040631643319697105464587e4
|
|
|
|
P0 = .89678597403663861959987488e3
|
|
|
|
Q4 = .5895697050844462222791e2
|
|
|
|
Q3 = .536265374031215315104235e3
|
|
|
|
Q2 = .16667838148816337184521798e4
|
|
|
|
Q1 = .207933497444540981287275926e4
|
|
|
|
Q0 = .89678597403663861962481162e3
|
2009-01-15 20:11:32 -07:00
|
|
|
)
|
2009-12-15 16:35:38 -07:00
|
|
|
sq := arg * arg
|
|
|
|
value := ((((P4*sq+P3)*sq+P2)*sq+P1)*sq + P0)
|
|
|
|
value = value / (((((sq+Q4)*sq+Q3)*sq+Q2)*sq+Q1)*sq + Q0)
|
|
|
|
return value * arg
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-07-08 21:48:41 -06:00
|
|
|
* satan reduces its argument (known to be positive)
|
|
|
|
* to the range [0,0.414...] and calls xatan.
|
2008-03-28 14:56:47 -06:00
|
|
|
*/
|
2009-01-15 17:16:42 -07:00
|
|
|
func satan(arg float64) float64 {
|
2009-10-06 20:40:35 -06:00
|
|
|
if arg < Sqrt2-1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return xatan(arg)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
if arg > Sqrt2+1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return Pi/2 - xatan(1/arg)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return Pi/4 + xatan((arg-1)/(arg+1))
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-01-08 15:12:10 -07:00
|
|
|
* Atan makes its argument positive and
|
2008-07-08 21:48:41 -06:00
|
|
|
* calls the inner routine satan.
|
2008-03-28 14:56:47 -06:00
|
|
|
*/
|
2009-03-05 14:31:01 -07:00
|
|
|
|
|
|
|
// Atan returns the arc tangent of x.
|
|
|
|
func Atan(x float64) float64 {
|
|
|
|
if x > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return satan(x)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return -satan(-x)
|
2008-03-28 14:56:47 -06:00
|
|
|
}
|