github.com/primecitizens/pcz/std@v0.2.1/math/sinh.go (about)

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