sinh.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 hyperbolic sine and cosine.
  7. The exponential func is called for arguments
  8. greater in magnitude than 0.5.
  9. A series is used for arguments smaller in magnitude than 0.5.
  10. Cosh(x) is computed from the exponential func for
  11. all arguments.
  12. */
  13. // Sinh returns the hyperbolic sine of x.
  14. //
  15. // Special cases are:
  16. // Sinh(±0) = ±0
  17. // Sinh(±Inf) = ±Inf
  18. // Sinh(NaN) = NaN
  19. func Sinh(x float64) float64 {
  20. // The coefficients are #2029 from Hart & Cheney. (20.36D)
  21. const (
  22. P0 = -0.6307673640497716991184787251e+6
  23. P1 = -0.8991272022039509355398013511e+5
  24. P2 = -0.2894211355989563807284660366e+4
  25. P3 = -0.2630563213397497062819489e+2
  26. Q0 = -0.6307673640497716991212077277e+6
  27. Q1 = 0.1521517378790019070696485176e+5
  28. Q2 = -0.173678953558233699533450911e+3
  29. )
  30. sign := false
  31. if x < 0 {
  32. x = -x
  33. sign = true
  34. }
  35. var temp float64
  36. switch true {
  37. case x > 21:
  38. temp = Exp(x) / 2
  39. case x > 0.5:
  40. temp = (Exp(x) - Exp(-x)) / 2
  41. default:
  42. sq := x * x
  43. temp = (((P3*sq+P2)*sq+P1)*sq + P0) * x
  44. temp = temp / (((sq+Q2)*sq+Q1)*sq + Q0)
  45. }
  46. if sign {
  47. temp = -temp
  48. }
  49. return temp
  50. }
  51. // Cosh returns the hyperbolic cosine of x.
  52. //
  53. // Special cases are:
  54. // Cosh(±0) = 1
  55. // Cosh(±Inf) = +Inf
  56. // Cosh(NaN) = NaN
  57. func Cosh(x float64) float64 {
  58. if x < 0 {
  59. x = -x
  60. }
  61. if x > 21 {
  62. return Exp(x) / 2
  63. }
  64. return (Exp(x) + Exp(-x)) / 2
  65. }