asin.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. package math
  5. /*
  6. Floating-point arcsine and arccosine.
  7. They are implemented by computing the arctangent
  8. after appropriate range reduction.
  9. */
  10. // Asin returns the arcsine, in radians, of x.
  11. //
  12. // Special cases are:
  13. // Asin(±0) = ±0
  14. // Asin(x) = NaN if x < -1 or x > 1
  15. //extern asin
  16. func libc_asin(float64) float64
  17. func Asin(x float64) float64 {
  18. return libc_asin(x)
  19. }
  20. func asin(x float64) float64 {
  21. if x == 0 {
  22. return x // special case
  23. }
  24. sign := false
  25. if x < 0 {
  26. x = -x
  27. sign = true
  28. }
  29. if x > 1 {
  30. return NaN() // special case
  31. }
  32. temp := Sqrt(1 - x*x)
  33. if x > 0.7 {
  34. temp = Pi/2 - satan(temp/x)
  35. } else {
  36. temp = satan(x / temp)
  37. }
  38. if sign {
  39. temp = -temp
  40. }
  41. return temp
  42. }
  43. // Acos returns the arccosine, in radians, of x.
  44. //
  45. // Special case is:
  46. // Acos(x) = NaN if x < -1 or x > 1
  47. //extern acos
  48. func libc_acos(float64) float64
  49. func Acos(x float64) float64 {
  50. return libc_acos(x)
  51. }
  52. func acos(x float64) float64 {
  53. return Pi/2 - Asin(x)
  54. }