blob: 88b851e5508cbcbeaa25894023f30fb544b750d6 [file] [log] [blame]
Ken Thompson21810982008-03-28 13:56:47 -07001// 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
Rob Pike43312932008-06-27 17:06:23 -07005package math
Ken Thompson21810982008-03-28 13:56:47 -07006
Ken Thompson21810982008-03-28 13:56:47 -07007/*
Charles L. Doriana0690b62010-02-01 22:21:40 -08008 Floating-point arcsine and arccosine.
Ken Thompson21810982008-03-28 13:56:47 -07009
Rob Pike00e2cda2010-01-12 07:38:31 +110010 They are implemented by computing the arctangent
11 after appropriate range reduction.
12*/
13
Rob Pike97958822013-10-07 16:32:47 -070014// Asin returns the arcsine, in radians, of x.
Charles L. Doriana0690b62010-02-01 22:21:40 -080015//
Charles L. Dorian22f84c52010-04-26 22:44:39 -070016// Special cases are:
17// Asin(±0) = ±0
Charles L. Doriana0690b62010-02-01 22:21:40 -080018// Asin(x) = NaN if x < -1 or x > 1
Russ Coxdd8dc6f2011-12-13 15:20:12 -050019func Asin(x float64) float64
20
21func asin(x float64) float64 {
Charles L. Dorian22f84c52010-04-26 22:44:39 -070022 if x == 0 {
23 return x // special case
24 }
Robert Griesemera3d10452009-12-15 15:35:38 -080025 sign := false
Ken Thompson21810982008-03-28 13:56:47 -070026 if x < 0 {
Robert Griesemera3d10452009-12-15 15:35:38 -080027 x = -x
28 sign = true
Ken Thompson21810982008-03-28 13:56:47 -070029 }
Russ Coxdfc39102009-03-05 13:31:01 -080030 if x > 1 {
Charles L. Dorian22f84c52010-04-26 22:44:39 -070031 return NaN() // special case
Ken Thompson21810982008-03-28 13:56:47 -070032 }
33
Robert Griesemera3d10452009-12-15 15:35:38 -080034 temp := Sqrt(1 - x*x)
Ken Thompson21810982008-03-28 13:56:47 -070035 if x > 0.7 {
Charles L. Dorianfd1db672010-01-08 14:12:10 -080036 temp = Pi/2 - satan(temp/x)
Ken Thompson21810982008-03-28 13:56:47 -070037 } else {
Charles L. Dorianfd1db672010-01-08 14:12:10 -080038 temp = satan(x / temp)
Ken Thompson21810982008-03-28 13:56:47 -070039 }
40
41 if sign {
Robert Griesemer40621d52009-11-09 12:07:39 -080042 temp = -temp
Ken Thompson21810982008-03-28 13:56:47 -070043 }
Robert Griesemera3d10452009-12-15 15:35:38 -080044 return temp
Ken Thompson21810982008-03-28 13:56:47 -070045}
46
Rob Pike97958822013-10-07 16:32:47 -070047// Acos returns the arccosine, in radians, of x.
Charles L. Doriana0690b62010-02-01 22:21:40 -080048//
49// Special case is:
50// Acos(x) = NaN if x < -1 or x > 1
Russ Coxdd8dc6f2011-12-13 15:20:12 -050051func Acos(x float64) float64
52
53func acos(x float64) float64 {
54 return Pi/2 - Asin(x)
55}