amath  1.6.2
Simple command line calculator
atanh.c
Go to the documentation of this file.
1 /* @(#)e_atanh.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_atanh.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 #ifdef _WIN32
48 #pragma warning( disable : 4146 )
49 #endif
50 
51 static const double one = 1.0, huge = 1e300;
52 static double zero = 0.0;
53 
54 /**
55  * @brief Inverse hyperbolic tangent function.
56  * @version 1.3
57  * @date 95/01/18
58  * @details
59  * <pre>
60  * Method :
61  * 1.Reduced x to positive by atanh(-x) = -atanh(x)
62  * 2.For x>=0.5
63  * 1 2x x
64  * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
65  * 2 1 - x 1 - x
66  *
67  * For x<0.5
68  * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
69  *
70  * Special cases:
71  * atanh(x) is NaN if |x| > 1 with signal;
72  * atanh(NaN) is that NaN with no signal;
73  * atanh(+-1) is +-INF with signal.
74  * </pre>
75  * @copyright Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
76  * @license Developed at SunSoft, a Sun Microsystems, Inc. business. Permission
77  * to use, copy, modify, and distribute this software is freely granted,
78  * provided that this notice is preserved.
79  */
80 
81 double atanh(double x)
82 {
83  double t;
84  sword hx,ix;
85  uword lx;
86  GET_HIGH_WORD(hx,x);
87  GET_LOW_WORD(lx,x);
88  ix = hx&0x7fffffff;
89  if ((ix|((lx|(-lx))>>31))>0x3ff00000) /* |x|>1 */
90  return (x-x)/(x-x);
91  if(ix==0x3ff00000)
92  return x/zero;
93  if(ix<0x3e300000&&(huge+x)>zero) return x; /* x<2**-28 */
94  SET_HIGH_WORD(x, ix); /* x <- |x| */
95  if(ix<0x3fe00000) { /* x < 0.5 */
96  t = x+x;
97  t = 0.5*log1p(t+t*x/(one-x));
98  } else
99  t = 0.5*log1p((x+x)/(one-x));
100  if(hx>=0) return t;
101  else return -t;
102 }
#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
static double zero
Definition: atanh.c:52
double atanh(double x)
Inverse hyperbolic tangent function.
Definition: atanh.c:81
unsigned int uword
32 bit unsigned integer.
Definition: prim.h:101
static const double huge
Definition: atanh.c:51
static const double one
Definition: atanh.c:51
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:199