Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 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 | |
Rob Pike | 4331293 | 2008-06-27 17:06:23 -0700 | [diff] [blame] | 5 | package math |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 6 | |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 7 | /* |
Charles L. Dorian | a0690b6 | 2010-02-01 22:21:40 -0800 | [diff] [blame] | 8 | Floating-point arcsine and arccosine. |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 9 | |
Rob Pike | 00e2cda | 2010-01-12 07:38:31 +1100 | [diff] [blame] | 10 | They are implemented by computing the arctangent |
| 11 | after appropriate range reduction. |
| 12 | */ |
| 13 | |
Rob Pike | 9795882 | 2013-10-07 16:32:47 -0700 | [diff] [blame] | 14 | // Asin returns the arcsine, in radians, of x. |
Charles L. Dorian | a0690b6 | 2010-02-01 22:21:40 -0800 | [diff] [blame] | 15 | // |
Charles L. Dorian | 22f84c5 | 2010-04-26 22:44:39 -0700 | [diff] [blame] | 16 | // Special cases are: |
| 17 | // Asin(±0) = ±0 |
Charles L. Dorian | a0690b6 | 2010-02-01 22:21:40 -0800 | [diff] [blame] | 18 | // Asin(x) = NaN if x < -1 or x > 1 |
Russ Cox | dd8dc6f | 2011-12-13 15:20:12 -0500 | [diff] [blame] | 19 | func Asin(x float64) float64 |
| 20 | |
| 21 | func asin(x float64) float64 { |
Charles L. Dorian | 22f84c5 | 2010-04-26 22:44:39 -0700 | [diff] [blame] | 22 | if x == 0 { |
| 23 | return x // special case |
| 24 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 25 | sign := false |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 26 | if x < 0 { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 27 | x = -x |
| 28 | sign = true |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 29 | } |
Russ Cox | dfc3910 | 2009-03-05 13:31:01 -0800 | [diff] [blame] | 30 | if x > 1 { |
Charles L. Dorian | 22f84c5 | 2010-04-26 22:44:39 -0700 | [diff] [blame] | 31 | return NaN() // special case |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 34 | temp := Sqrt(1 - x*x) |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 35 | if x > 0.7 { |
Charles L. Dorian | fd1db67 | 2010-01-08 14:12:10 -0800 | [diff] [blame] | 36 | temp = Pi/2 - satan(temp/x) |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 37 | } else { |
Charles L. Dorian | fd1db67 | 2010-01-08 14:12:10 -0800 | [diff] [blame] | 38 | temp = satan(x / temp) |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if sign { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 42 | temp = -temp |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 43 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 44 | return temp |
Ken Thompson | 2181098 | 2008-03-28 13:56:47 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Rob Pike | 9795882 | 2013-10-07 16:32:47 -0700 | [diff] [blame] | 47 | // Acos returns the arccosine, in radians, of x. |
Charles L. Dorian | a0690b6 | 2010-02-01 22:21:40 -0800 | [diff] [blame] | 48 | // |
| 49 | // Special case is: |
| 50 | // Acos(x) = NaN if x < -1 or x > 1 |
Russ Cox | dd8dc6f | 2011-12-13 15:20:12 -0500 | [diff] [blame] | 51 | func Acos(x float64) float64 |
| 52 | |
| 53 | func acos(x float64) float64 { |
| 54 | return Pi/2 - Asin(x) |
| 55 | } |