The Go Programming Language

Source file src/pkg/math/tan.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 tangent.
     9	*/
    10	
    11	// Tan returns the tangent of x.
    12	func Tan(x float64) float64 {
    13		// Coefficients are #4285 from Hart & Cheney. (19.74D)
    14		const (
    15			P0 = -.1306820264754825668269611177e+5
    16			P1 = .1055970901714953193602353981e+4
    17			P2 = -.1550685653483266376941705728e+2
    18			P3 = .3422554387241003435328470489e-1
    19			P4 = .3386638642677172096076369e-4
    20			Q0 = -.1663895238947119001851464661e+5
    21			Q1 = .4765751362916483698926655581e+4
    22			Q2 = -.1555033164031709966900124574e+3
    23		)
    24	
    25		flag := false
    26		sign := false
    27		if x < 0 {
    28			x = -x
    29			sign = true
    30		}
    31		x = x * (4 / Pi) /* overflow? */
    32		var e float64
    33		e, x = Modf(x)
    34		i := int32(e)
    35	
    36		switch i & 3 {
    37		case 1:
    38			x = 1 - x
    39			flag = true
    40	
    41		case 2:
    42			sign = !sign
    43			flag = true
    44	
    45		case 3:
    46			x = 1 - x
    47			sign = !sign
    48		}
    49	
    50		xsq := x * x
    51		temp := ((((P4*xsq+P3)*xsq+P2)*xsq+P1)*xsq + P0) * x
    52		temp = temp / (((xsq+Q2)*xsq+Q1)*xsq + Q0)
    53	
    54		if flag {
    55			if temp == 0 {
    56				return NaN()
    57			}
    58			temp = 1 / temp
    59		}
    60		if sign {
    61			temp = -temp
    62		}
    63		return temp
    64	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.