asin.go 897 Bytes
Newer Older
Ken Thompson's avatar
Ken Thompson committed
1 2 3 4
// 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.

5 6
package math

Ken Thompson's avatar
Ken Thompson committed
7 8

/*
9
	Floating-point arcsine and arccosine.
Ken Thompson's avatar
Ken Thompson committed
10

11 12 13 14 15
	They are implemented by computing the arctangent
	after appropriate range reduction.
*/

// Asin returns the arcsine of x.
16
//
17 18
// Special cases are:
//	Asin(±0) = ±0
19
//	Asin(x) = NaN if x < -1 or x > 1
Russ Cox's avatar
Russ Cox committed
20
func Asin(x float64) float64 {
21 22 23
	if x == 0 {
		return x // special case
	}
24
	sign := false
Ken Thompson's avatar
Ken Thompson committed
25
	if x < 0 {
26 27
		x = -x
		sign = true
Ken Thompson's avatar
Ken Thompson committed
28
	}
Russ Cox's avatar
Russ Cox committed
29
	if x > 1 {
30
		return NaN() // special case
Ken Thompson's avatar
Ken Thompson committed
31 32
	}

33
	temp := Sqrt(1 - x*x)
Ken Thompson's avatar
Ken Thompson committed
34
	if x > 0.7 {
35
		temp = Pi/2 - satan(temp/x)
Ken Thompson's avatar
Ken Thompson committed
36
	} else {
37
		temp = satan(x / temp)
Ken Thompson's avatar
Ken Thompson committed
38 39 40
	}

	if sign {
41
		temp = -temp
Ken Thompson's avatar
Ken Thompson committed
42
	}
43
	return temp
Ken Thompson's avatar
Ken Thompson committed
44 45
}

46
// Acos returns the arccosine of x.
47 48 49
//
// Special case is:
//	Acos(x) = NaN if x < -1 or x > 1
50
func Acos(x float64) float64 { return Pi/2 - Asin(x) }