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

     1  // Copyright 2010 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  // The original C code, the long comment, and the constants
     8  // below are from FreeBSD's /usr/src/lib/msun/src/s_asinh.c
     9  // and came with this notice. The go code is a simplified
    10  // version of the original C.
    11  //
    12  // ====================================================
    13  // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
    14  //
    15  // Developed at SunPro, a Sun Microsystems, Inc. business.
    16  // Permission to use, copy, modify, and distribute this
    17  // software is freely granted, provided that this notice
    18  // is preserved.
    19  // ====================================================
    20  //
    21  //
    22  // asinh(x)
    23  // Method :
    24  //	Based on
    25  //	        asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
    26  //	we have
    27  //	asinh(x) := x  if  1+x*x=1,
    28  //	         := sign(x)*(log(x)+ln2)) for large |x|, else
    29  //	         := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
    30  //	         := sign(x)*log1p(|x| + x**2/(1 + sqrt(1+x**2)))
    31  //
    32  
    33  // Asinh returns the inverse hyperbolic sine of x.
    34  //
    35  // Special cases are:
    36  //
    37  //	Asinh(±0) = ±0
    38  //	Asinh(±Inf) = ±Inf
    39  //	Asinh(NaN) = NaN
    40  func Asinh(x float64) float64 {
    41  	return asinh(x)
    42  }
    43  
    44  func asinh(x float64) float64 {
    45  	const (
    46  		Ln2      = 6.93147180559945286227e-01 // 0x3FE62E42FEFA39EF
    47  		NearZero = 1.0 / (1 << 28)            // 2**-28
    48  		Large    = 1 << 28                    // 2**28
    49  	)
    50  	// special cases
    51  	if IsNaN(x) || IsInf(x, 0) {
    52  		return x
    53  	}
    54  	sign := false
    55  	if x < 0 {
    56  		x = -x
    57  		sign = true
    58  	}
    59  	var temp float64
    60  	switch {
    61  	case x > Large:
    62  		temp = Log(x) + Ln2 // |x| > 2**28
    63  	case x > 2:
    64  		temp = Log(2*x + 1/(Sqrt(x*x+1)+x)) // 2**28 > |x| > 2.0
    65  	case x < NearZero:
    66  		temp = x // |x| < 2**-28
    67  	default:
    68  		temp = Log1p(x + x*x/(1+Sqrt(1+x*x))) // 2.0 > |x| > 2**-28
    69  	}
    70  	if sign {
    71  		temp = -temp
    72  	}
    73  	return temp
    74  }