amath  1.6.2
Simple command line calculator
acosh.c
Go to the documentation of this file.
1 /* @(#)e_acosh.c 1.3 95/01/18 */
2 
3 /*
4  * Copyright (c) 2015-2017 Carsten Sonne Larsen <cs@innolan.dk>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The origin source code can be obtained from:
28  * http://www.netlib.org/fdlibm/e_acosh.c
29  *
30  */
31 
32 /*
33  * ====================================================
34  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
35  *
36  * Developed at SunSoft, a Sun Microsystems, Inc. business.
37  * Permission to use, copy, modify, and distribute this
38  * software is freely granted, provided that this notice
39  * is preserved.
40  * ====================================================
41  *
42  */
43 
44 #include "prim.h"
45 #include "math.h"
46 
47 static const double
48 one = 1.0,
49 ln2 = 6.93147180559945286227e-01; /* 0x3FE62E42, 0xFEFA39EF */
50 
51 /**
52  * @brief Inverse hyperbolic cosine function.
53  * @version 1.3
54  * @date 95/01/18
55  * @details
56  * <pre>
57  * Method :
58  * Based on
59  * acosh(x) = log [ x + sqrt(x*x-1) ]
60  * we have
61  * acosh(x) := log(x)+ln2, if x is large; else
62  * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
63  * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
64  *
65  * Special cases:
66  * acosh(x) is NaN with signal if x<1.
67  * acosh(NaN) is NaN without signal.
68  * </pre>
69  * @copyright Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
70  * @license Developed at SunSoft, a Sun Microsystems, Inc. business. Permission
71  * to use, copy, modify, and distribute this software is freely granted,
72  * provided that this notice is preserved.
73  */
74 
75 double acosh(double x)
76 {
77  double t;
78  sword hx, lx;
79  GET_HIGH_WORD(hx,x);
80  GET_LOW_WORD(lx,x);
81 
82  if(hx<0x3ff00000) { /* x < 1 */
83  return (x-x)/(x-x);
84  } else if(hx >=0x41b00000) { /* x > 2**28 */
85  if(hx >=0x7ff00000) { /* x is inf of NaN */
86  return x+x;
87  } else
88  return log(x)+ln2; /* acosh(huge)=log(2x) */
89  } else if(((hx-0x3ff00000)|lx)==0) {
90  return 0.0; /* acosh(1) = 0 */
91  } else if (hx > 0x40000000) { /* 2**28 > x > 2 */
92  t=x*x;
93  return log(2.0*x-one/(x+sqrt(t-one)));
94  } else { /* 1<x<2 */
95  t = x-one;
96  return log1p(t+sqrt(2.0*t+t*t));
97  }
98 }
double sqrt(double x)
Square root function.
Definition: sqrt.c:127
static const double one
Definition: acosh.c:48
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:165
double log1p(double x)
Definition: log1p.c:125
#define GET_LOW_WORD(i, d)
Get the less significant 32 bit int from a double.
Definition: prim.h:176
signed int sword
32 bit signed integer.
Definition: prim.h:107
double acosh(double x)
Inverse hyperbolic cosine function.
Definition: acosh.c:75
static const double ln2
Definition: acosh.c:49