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 
     27 func sinh(x float64) float64 {
     28 	// The coefficients are #2029 from Hart & Cheney. (20.36D)
     29 	const (
     30 		P0 = -0.6307673640497716991184787251e+6
     31 		P1 = -0.8991272022039509355398013511e+5
     32 		P2 = -0.2894211355989563807284660366e+4
     33 		P3 = -0.2630563213397497062819489e+2
     34 		Q0 = -0.6307673640497716991212077277e+6
     35 		Q1 = 0.1521517378790019070696485176e+5
     36 		Q2 = -0.173678953558233699533450911e+3
     37 	)
     38 
     39 	sign := false
     40 	if x < 0 {
     41 		x = -x
     42 		sign = true
     43 	}
     44 
     45 	var temp float64
     46 	switch true {
     47 	case x > 21:
     48 		temp = Exp(x) / 2
     49 
     50 	case x > 0.5:
     51 		temp = (Exp(x) - Exp(-x)) / 2
     52 
     53 	default:
     54 		sq := x * x
     55 		temp = (((P3*sq+P2)*sq+P1)*sq + P0) * x
     56 		temp = temp / (((sq+Q2)*sq+Q1)*sq + Q0)
     57 	}
     58 
     59 	if sign {
     60 		temp = -temp
     61 	}
     62 	return temp
     63 }
     64 
     65 // Cosh returns the hyperbolic cosine of x.
     66 //
     67 // Special cases are:
     68 //	Cosh(0) = 1
     69 //	Cosh(Inf) = +Inf
     70 //	Cosh(NaN) = NaN
     71 func Cosh(x float64) float64
     72 
     73 func cosh(x float64) float64 {
     74 	if x < 0 {
     75 		x = -x
     76 	}
     77 	if x > 21 {
     78 		return Exp(x) / 2
     79 	}
     80 	return (Exp(x) + Exp(-x)) / 2
     81 }
     82