The Go Programming Language

Source file src/pkg/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	func Sinh(x float64) float64 {
    21		// The coefficients are #2029 from Hart & Cheney. (20.36D)
    22		const (
    23			P0 = -0.6307673640497716991184787251e+6
    24			P1 = -0.8991272022039509355398013511e+5
    25			P2 = -0.2894211355989563807284660366e+4
    26			P3 = -0.2630563213397497062819489e+2
    27			Q0 = -0.6307673640497716991212077277e+6
    28			Q1 = 0.1521517378790019070696485176e+5
    29			Q2 = -0.173678953558233699533450911e+3
    30		)
    31	
    32		sign := false
    33		if x < 0 {
    34			x = -x
    35			sign = true
    36		}
    37	
    38		var temp float64
    39		switch true {
    40		case x > 21:
    41			temp = Exp(x) / 2
    42	
    43		case x > 0.5:
    44			temp = (Exp(x) - Exp(-x)) / 2
    45	
    46		default:
    47			sq := x * x
    48			temp = (((P3*sq+P2)*sq+P1)*sq + P0) * x
    49			temp = temp / (((sq+Q2)*sq+Q1)*sq + Q0)
    50		}
    51	
    52		if sign {
    53			temp = -temp
    54		}
    55		return temp
    56	}
    57	
    58	// Cosh returns the hyperbolic cosine of x.
    59	func Cosh(x float64) float64 {
    60		if x < 0 {
    61			x = -x
    62		}
    63		if x > 21 {
    64			return Exp(x) / 2
    65		}
    66		return (Exp(x) + Exp(-x)) / 2
    67	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.