src/pkg/math/atan.go - The Go Programming Language

Golang

Source file src/pkg/math/atan.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package math
     6	
     7	/*
     8		Floating-point arctangent.
     9	
    10		Atan returns the value of the arctangent of its
    11		argument in the range [-pi/2,pi/2].
    12		There are no error returns.
    13		Coefficients are #5077 from Hart & Cheney. (19.56D)
    14	*/
    15	
    16	// xatan evaluates a series valid in the
    17	// range [-0.414...,+0.414...]. (tan(pi/8))
    18	func xatan(arg float64) float64 {
    19		const (
    20			P4 = .161536412982230228262e2
    21			P3 = .26842548195503973794141e3
    22			P2 = .11530293515404850115428136e4
    23			P1 = .178040631643319697105464587e4
    24			P0 = .89678597403663861959987488e3
    25			Q4 = .5895697050844462222791e2
    26			Q3 = .536265374031215315104235e3
    27			Q2 = .16667838148816337184521798e4
    28			Q1 = .207933497444540981287275926e4
    29			Q0 = .89678597403663861962481162e3
    30		)
    31		sq := arg * arg
    32		value := ((((P4*sq+P3)*sq+P2)*sq+P1)*sq + P0)
    33		value = value / (((((sq+Q4)*sq+Q3)*sq+Q2)*sq+Q1)*sq + Q0)
    34		return value * arg
    35	}
    36	
    37	// satan reduces its argument (known to be positive)
    38	// to the range [0,0.414...] and calls xatan.
    39	func satan(arg float64) float64 {
    40		if arg < Sqrt2-1 {
    41			return xatan(arg)
    42		}
    43		if arg > Sqrt2+1 {
    44			return Pi/2 - xatan(1/arg)
    45		}
    46		return Pi/4 + xatan((arg-1)/(arg+1))
    47	}
    48	
    49	// Atan returns the arctangent of x.
    50	//
    51	// Special cases are:
    52	//	Atan(±0) = ±0
    53	//	Atan(±Inf) = ±Pi/2
    54	func Atan(x float64) float64
    55	
    56	func atan(x float64) float64 {
    57		if x == 0 {
    58			return x
    59		}
    60		if x > 0 {
    61			return satan(x)
    62		}
    63		return -satan(-x)
    64	}