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