src/pkg/math/tanh.go - The Go Programming Language

Golang

Source file src/pkg/math/tanh.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 tangent.
     9	
    10		Sinh and Cosh are called except for large arguments, which
    11		would cause overflow improperly.
    12	*/
    13	
    14	// Tanh computes the hyperbolic tangent of x.
    15	//
    16	// Special cases are:
    17	//	Tanh(±0) = ±0
    18	//	Tanh(±Inf) = ±1
    19	//	Tanh(NaN) = NaN
    20	func Tanh(x float64) float64 {
    21		if x < 0 {
    22			x = -x
    23			if x > 21 {
    24				return -1
    25			}
    26			return -Sinh(x) / Cosh(x)
    27		}
    28		if x > 21 {
    29			return 1
    30		}
    31		return Sinh(x) / Cosh(x)
    32	}