asinh.go 1.7 KB

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