summaryrefslogtreecommitdiffstats
path: root/Python/pymath.c
blob: 7c0010675a332e3ca816424079a470287715d8de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "Python.h"

#ifndef HAVE_HYPOT
double hypot(double x, double y)
{
	double yx;

	x = fabs(x);
	y = fabs(y);
	if (x < y) {
		double temp = x;
		x = y;
		y = temp;
	}
	if (x == 0.)
		return 0.;
	else {
		yx = y/x;
		return x*sqrt(1.+yx*yx);
	}
}
#endif /* HAVE_HYPOT */

#ifndef HAVE_COPYSIGN
static double
copysign(double x, double y)
{
	/* use atan2 to distinguish -0. from 0. */
	if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
		return fabs(x);
	} else {
		return -fabs(x);
	}
}
#endif /* HAVE_COPYSIGN */

#ifndef HAVE_LOG1P
double
log1p(double x)
{
	/* For x small, we use the following approach.  Let y be the nearest
	   float to 1+x, then

	     1+x = y * (1 - (y-1-x)/y)

	   so log(1+x) = log(y) + log(1-(y-1-x)/y).  Since (y-1-x)/y is tiny,
	   the second term is well approximated by (y-1-x)/y.  If abs(x) >=
	   DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
	   then y-1-x will be exactly representable, and is computed exactly
	   by (y-1)-x.

	   If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
	   round-to-nearest then this method is slightly dangerous: 1+x could
	   be rounded up to 1+DBL_EPSILON instead of down to 1, and in that
	   case y-1-x will not be exactly representable any more and the
	   result can be off by many ulps.  But this is easily fixed: for a
	   floating-point number |x| < DBL_EPSILON/2., the closest
	   floating-point number to log(1+x) is exactly x.
	*/

	double y;
	if (fabs(x) < DBL_EPSILON/2.) {
		return x;
	} else if (-0.5 <= x && x <= 1.) {
		/* WARNING: it's possible than an overeager compiler
		   will incorrectly optimize the following two lines
		   to the equivalent of "return log(1.+x)". If this
		   happens, then results from log1p will be inaccurate
		   for small x. */
		y = 1.+x;
		return log(y)-((y-1.)-x)/y;
	} else {
		/* NaNs and infinities should end up here */
		return log(1.+x);
	}
}
#endif /* HAVE_LOG1P */

/*
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice 
 * is preserved.
 * ====================================================
 */

static const double ln2 = 6.93147180559945286227E-01;
static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
static const double two_pow_p28 = 268435456.0; /* 2**28 */
static const double zero = 0.0;

/* asinh(x)
 * Method :
 *	Based on 
 *		asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
 *	we have
 *	asinh(x) := x  if  1+x*x=1,
 *		 := sign(x)*(log(x)+ln2)) for large |x|, else
 *		 := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
 *		 := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))  
 */

#ifndef HAVE_ASINH
double
asinh(double x)
{	
	double w;
	double absx = fabs(x);

	if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
		return x+x;
	}
	if (absx < two_pow_m28) {	/* |x| < 2**-28 */
		return x;	/* return x inexact except 0 */
	} 
	if (absx > two_pow_p28) {	/* |x| > 2**28 */
		w = log(absx)+ln2;
	}
	else if (absx > 2.0) {		/* 2 < |x| < 2**28 */
		w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
	}
	else {				/* 2**-28 <= |x| < 2= */
		double t = x*x;
		w = log1p(absx + t / (1.0 + sqrt(1.0 + t)));
	}
	return copysign(w, x);
	
}
#endif /* HAVE_ASINH */

/* acosh(x)
 * Method :
 *      Based on
 *	      acosh(x) = log [ x + sqrt(x*x-1) ]
 *      we have
 *	      acosh(x) := log(x)+ln2, if x is large; else
 *	      acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
 *	      acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
 *
 * Special cases:
 *      acosh(x) is NaN with signal if x<1.
 *      acosh(NaN) is NaN without signal.
 */

#ifndef HAVE_ACOSH
double
acosh(double x)
{
	if (Py_IS_NAN(x)) {
		return x+x;
	}
	if (x < 1.) {			/* x < 1;  return a signaling NaN */
		errno = EDOM;
#ifdef Py_NAN
		return Py_NAN;
#else
		return (x-x)/(x-x);
#endif
	}
	else if (x >= two_pow_p28) {	/* x > 2**28 */
		if (Py_IS_INFINITY(x)) {
			return x+x;
		} else {
			return log(x)+ln2;	/* acosh(huge)=log(2x) */
		}
	}
	else if (x == 1.) {
		return 0.0;			/* acosh(1) = 0 */
	}
	else if (x > 2.) {			/* 2 < x < 2**28 */
		double t = x*x;
		return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
	}
	else {				/* 1 < x <= 2 */
		double t = x - 1.0;
		return log1p(t + sqrt(2.0*t + t*t));
	}
}
#endif /* HAVE_ACOSH */

/* atanh(x)
 * Method :
 *    1.Reduced x to positive by atanh(-x) = -atanh(x)
 *    2.For x>=0.5
 *		  1	      2x			  x
 *      atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
 *		  2	     1 - x		      1 - x
 *
 *      For x<0.5
 *      atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
 *
 * Special cases:
 *      atanh(x) is NaN if |x| >= 1 with signal;
 *      atanh(NaN) is that NaN with no signal;
 *
 */

#ifndef HAVE_ATANH
double
atanh(double x)
{
	double absx;
	double t;

	if (Py_IS_NAN(x)) {
		return x+x;
	}
	absx = fabs(x);
	if (absx >= 1.) {		/* |x| >= 1 */
		errno = EDOM;
#ifdef Py_NAN
		return Py_NAN;
#else
		return x/zero;
#endif
	}
	if (absx < two_pow_m28) {	/* |x| < 2**-28 */
		return x;
	}
	if (absx < 0.5) {		/* |x| < 0.5 */
		t = absx+absx;
		t = 0.5 * log1p(t + t*absx / (1.0 - absx));
	} 
	else {				/* 0.5 <= |x| <= 1.0 */
		t = 0.5 * log1p((absx + absx) / (1.0 - absx));
	}
	return copysign(t, x);
}
#endif /* HAVE_ATANH */
Tcl is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful.
summaryrefslogtreecommitdiffstats
path: root/tools
Commit message (Expand)AuthorAgeFilesLines
* Fix [3bd69eba99a395ee]: 'make dist' fails when tclsh9.0 is on $PATHjan.nijtmans2016-06-016-11/+1
|\
| * Fix [3bd69eba99a395ee]: 'make dist' fails when tclsh9.0 is on $PATHjan.nijtmans2016-06-016-11/+1
| |\
| | * Fix [3bd69eba99a395ee]: 'make dist' fails when tclsh9.0 is on $PATHjan.nijtmans2016-06-016-12/+0
| | * Bump to release number 8.5.19dgp2015-10-231-1/+1
* | | configure.in -> configure.acjan.nijtmans2016-03-083-1131/+1826
* | | Bump trunk to 8.7a0 to accept new feature development.dgp2016-03-031-2/+2
|/ /
* | Decorate Tcl_Panic and Tcl_PanicVA with the noreturn option, alowing further ...panic_noreturnjan.nijtmans2015-09-221-0/+2
* | Various Unicode handling enhancements, when building with TCL_UTF_MAX > 3, in...jan.nijtmans2015-09-011-1/+5
* | Whitespace reduction in Tcl scripts. No functional change.jan.nijtmans2015-09-017-95/+95
* | Fix bug in "make dist" when system-encoding is UTF-8: eolFix will then transl...jan.nijtmans2015-06-252-21/+21
|\ \ | |/
| * Fix bug in "make dist" when system-encoding is UTF-8: eolFix will then transl...jan.nijtmans2015-06-253-2397/+2397
| * Bump to version 8.5.18.dgp2015-02-051-1/+1
| * Bump to 8.5.16 for release.dgp2014-07-231-1/+1
* | `make html` must tolerate bundled packages using configure.ac over configure.in.dgp2015-03-061-1/+5
* | Eliminate all usage of WIN32 and __WIN32__ macros: Some compilers (e.g. Clang...jan.nijtmans2014-02-101-3/+3
* | Improve descriptions of character escapes and ranges in Tcl.n.dkf2013-12-152-1/+2
* | Put extern "C" guards around all stub table struct definitions, so it is usab...jan.nijtmans2013-11-041-2/+3
|\ \ | |/
| * Put extern "C" guards around all stub table struct definitions, so it is usab...jan.nijtmans2013-11-041-2/+3
* | [01b77111e5]: Small fixes relating to this bug. In particular, the package namedkf2013-10-282-3/+8
* | Fix execute permission on many files which shouldn't have it.jan.nijtmans2013-10-172-0/+0
|\ \ | |/
| * Fix execute permission on many files which shouldn't have it.jan.nijtmans2013-10-172-0/+0
| * Bump to 8.5.15 for release.dgp2013-08-291-1/+1
* | Fix uniClass tool which was the real cause for [a876646efe], and add test-cas...jan.nijtmans2013-06-18