| 1 | module math |
| 2 | |
| 3 | // sinh calculates hyperbolic sine. |
| 4 | pub fn sinh(x_ f64) f64 { |
| 5 | mut x := x_ |
| 6 | // The coefficients are #2029 from Hart & Cheney. (20.36D) |
| 7 | p0 := -0.6307673640497716991184787251e+6 |
| 8 | p1 := -0.8991272022039509355398013511e+5 |
| 9 | p2 := -0.2894211355989563807284660366e+4 |
| 10 | p3 := -0.2630563213397497062819489e+2 |
| 11 | q0 := -0.6307673640497716991212077277e+6 |
| 12 | q1 := 0.1521517378790019070696485176e+5 |
| 13 | q2 := -0.173678953558233699533450911e+3 |
| 14 | mut neg := false |
| 15 | if x < 0 { |
| 16 | x = -x |
| 17 | neg = true |
| 18 | } |
| 19 | mut temp := 0.0 |
| 20 | if x > 21 { |
| 21 | temp = exp(x) * 0.5 |
| 22 | } else if x > 0.5 { |
| 23 | ex := exp(x) |
| 24 | temp = (ex - 1.0 / ex) * 0.5 |
| 25 | } else { |
| 26 | sq := x * x |
| 27 | temp = (((p3 * sq + p2) * sq + p1) * sq + p0) * x |
| 28 | temp = temp / (((sq + q2) * sq + q1) * sq + q0) |
| 29 | } |
| 30 | if neg { |
| 31 | temp = -temp |
| 32 | } |
| 33 | return temp |
| 34 | } |
| 35 | |
| 36 | // cosh returns the hyperbolic cosine of x in radians |
| 37 | // |
| 38 | // special cases are: |
| 39 | // cosh(±0) = 1 |
| 40 | // cosh(±inf) = +inf |
| 41 | // cosh(nan) = nan |
| 42 | pub fn cosh(x f64) f64 { |
| 43 | abs_x := abs(x) |
| 44 | if abs_x > 21 { |
| 45 | return exp(abs_x) * 0.5 |
| 46 | } |
| 47 | ex := exp(abs_x) |
| 48 | return (ex + 1.0 / ex) * 0.5 |
| 49 | } |
| 50 | |