Source file src/math/sinh.go

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

View as plain text