asinh.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2010 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. // The original C code, the long comment, and the constants
  6. // below are from FreeBSD's /usr/src/lib/msun/src/s_asinh.c
  7. // and came with this notice. The go code is a simplified
  8. // version of the original C.
  9. //
  10. // ====================================================
  11. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  12. //
  13. // Developed at SunPro, a Sun Microsystems, Inc. business.
  14. // Permission to use, copy, modify, and distribute this
  15. // software is freely granted, provided that this notice
  16. // is preserved.
  17. // ====================================================
  18. //
  19. //
  20. // asinh(x)
  21. // Method :
  22. // Based on
  23. // asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
  24. // we have
  25. // asinh(x) := x if 1+x*x=1,
  26. // := sign(x)*(log(x)+ln2)) for large |x|, else
  27. // := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
  28. // := sign(x)*log1p(|x| + x**2/(1 + sqrt(1+x**2)))
  29. //
  30. // Asinh returns the inverse hyperbolic sine of x.
  31. //
  32. // Special cases are:
  33. // Asinh(±0) = ±0
  34. // Asinh(±Inf) = ±Inf
  35. // Asinh(NaN) = NaN
  36. func Asinh(x float64) float64 {
  37. const (
  38. Ln2 = 6.93147180559945286227e-01 // 0x3FE62E42FEFA39EF
  39. NearZero = 1.0 / (1 << 28) // 2**-28
  40. Large = 1 << 28 // 2**28
  41. )
  42. // special cases
  43. if IsNaN(x) || IsInf(x, 0) {
  44. return x
  45. }
  46. sign := false
  47. if x < 0 {
  48. x = -x
  49. sign = true
  50. }
  51. var temp float64
  52. switch {
  53. case x > Large:
  54. temp = Log(x) + Ln2 // |x| > 2**28
  55. case x > 2:
  56. temp = Log(2*x + 1/(Sqrt(x*x+1)+x)) // 2**28 > |x| > 2.0
  57. case x < NearZero:
  58. temp = x // |x| < 2**-28
  59. default:
  60. temp = Log1p(x + x*x/(1+Sqrt(1+x*x))) // 2.0 > |x| > 2**-28
  61. }
  62. if sign {
  63. temp = -temp
  64. }
  65. return temp
  66. }