~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/math/acosh.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
8
// The original C code, the long comment, and the constants
 
9
// below are from FreeBSD's /usr/src/lib/msun/src/e_acosh.c
 
10
// and came with this notice.  The go code is a simplified
 
11
// version of the original C.
 
12
//
 
13
// ====================================================
 
14
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 
15
//
 
16
// Developed at SunPro, a Sun Microsystems, Inc. business.
 
17
// Permission to use, copy, modify, and distribute this
 
18
// software is freely granted, provided that this notice
 
19
// is preserved.
 
20
// ====================================================
 
21
//
 
22
//
 
23
// __ieee754_acosh(x)
 
24
// Method :
 
25
//      Based on
 
26
//              acosh(x) = log [ x + sqrt(x*x-1) ]
 
27
//      we have
 
28
//              acosh(x) := log(x)+ln2, if x is large; else
 
29
//              acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
 
30
//              acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
 
31
//
 
32
// Special cases:
 
33
//      acosh(x) is NaN with signal if x<1.
 
34
//      acosh(NaN) is NaN without signal.
 
35
//
 
36
 
 
37
// Acosh(x) calculates the inverse hyperbolic cosine of x.
 
38
//
 
39
// Special cases are:
 
40
//      Acosh(x) = NaN if x < 1
 
41
//      Acosh(NaN) = NaN
 
42
func Acosh(x float64) float64 {
 
43
        const (
 
44
                Ln2   = 6.93147180559945286227e-01 // 0x3FE62E42FEFA39EF
 
45
                Large = 1 << 28                    // 2**28
 
46
        )
 
47
        // TODO(rsc): Remove manual inlining of IsNaN
 
48
        // when compiler does it for us
 
49
        // first case is special case
 
50
        switch {
 
51
        case x < 1 || x != x: // x < 1 || IsNaN(x):
 
52
                return NaN()
 
53
        case x == 1:
 
54
                return 0
 
55
        case x >= Large:
 
56
                return Log(x) + Ln2 // x > 2**28
 
57
        case x > 2:
 
58
                return Log(2*x - 1/(x+Sqrt(x*x-1))) // 2**28 > x > 2
 
59
        }
 
60
        t := x - 1
 
61
        return Log1p(t + Sqrt(2*t+t*t)) // 2 >= x > 1
 
62
}