annotate Modules/cmathmodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents 844fde600a61
children 2e6da2dd2ab8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Complex math module */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* much code borrowed from mathmodule.c */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
5 #include "Python.h"
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #ifdef i860
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #undef HUGE_VAL
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #ifdef HUGE_VAL
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #define CHECK(x) if (errno != 0) ; \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 else errno = ERANGE
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #else
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #define CHECK(x) /* Don't know how to check */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 #ifndef M_PI
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 #define M_PI (3.141592653589793239)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 /* First, the C functions that do the real work */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 /* constants */
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
27 static Py_complex c_one = {1., 0.};
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
28 static Py_complex c_half = {0.5, 0.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
29 static Py_complex c_i = {0., 1.};
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
30 static Py_complex c_halfi = {0., 0.5};
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* forward declarations */
13685
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
33 staticforward Py_complex c_log(Py_complex);
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
34 staticforward Py_complex c_prodi(Py_complex);
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
35 staticforward Py_complex c_sqrt(Py_complex);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
38 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
39 c_acos(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
42 c_sqrt(c_diff(c_one,c_prod(x,x))))))));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
45 static char c_acos_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
46 "acos(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
47 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
48 "Return the arc cosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
49
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
50
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
51 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
52 c_acosh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 {
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
54 Py_complex z;
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
55 z = c_sqrt(c_half);
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
56 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_one)),
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
57 c_sqrt(c_diff(x,c_one)))));
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
58 return c_sum(z, z);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
61 static char c_acosh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
62 "acosh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
63 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
64 "Return the hyperbolic arccosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
65
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
66
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
67 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
68 c_asin(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 {
16722
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
70 /* -i * log[(sqrt(1-x**2) + i*x] */
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
71 const Py_complex squared = c_prod(x, x);
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
72 const Py_complex sqrt_1_minus_x_sq = c_sqrt(c_diff(c_one, squared));
16738
844fde600a61 asin micro-optimization suggested in email.
Tim Peters <tim.peters@gmail.com>
parents: 16722
diff changeset
73 return c_neg(c_prodi(c_log(
844fde600a61 asin micro-optimization suggested in email.
Tim Peters <tim.peters@gmail.com>
parents: 16722
diff changeset
74 c_sum(sqrt_1_minus_x_sq, c_prodi(x))
844fde600a61 asin micro-optimization suggested in email.
Tim Peters <tim.peters@gmail.com>
parents: 16722
diff changeset
75 ) ) );
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
78 static char c_asin_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
79 "asin(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
80 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
81 "Return the arc sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
82
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
83
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
84 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
85 c_asinh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 {
9886
fae6d24b897b Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
87 Py_complex z;
16722
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
88 z = c_sqrt(c_half);
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
89 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x, c_i)),
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
90 c_sqrt(c_diff(x, c_i)))));
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
91 return c_sum(z, z);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
94 static char c_asinh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
95 "asinh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
96 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
97 "Return the hyperbolic arc sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
98
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
99
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
100 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
101 c_atan(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 {
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
103 return c_prod(c_halfi,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
106 static char c_atan_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
107 "atan(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
108 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
109 "Return the arc tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
110
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
111
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
112 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
113 c_atanh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 {
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
115 return c_prod(c_half,c_log(c_quot(c_sum(c_one,x),c_diff(c_one,x))));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
118 static char c_atanh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
119 "atanh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
120 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
121 "Return the hyperbolic arc tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
122
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
123
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
124 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
125 c_cos(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
127 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 r.real = cos(x.real)*cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 r.imag = -sin(x.real)*sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
133 static char c_cos_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
134 "cos(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
135 "n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
136 "Return the cosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
137
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
138
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
139 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
140 c_cosh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
142 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 r.real = cos(x.imag)*cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 r.imag = sin(x.imag)*sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
148 static char c_cosh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
149 "cosh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
150 "n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
151 "Return the hyperbolic cosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
152
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
153
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
154 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
155 c_exp(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
157 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 double l = exp(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 r.real = l*cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 r.imag = l*sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
164 static char c_exp_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
165 "exp(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
166 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
167 "Return the exponential value e**x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
168
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
169
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
170 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
171 c_log(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
173 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 double l = hypot(x.real,x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 r.imag = atan2(x.imag, x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 r.real = log(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
180 static char c_log_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
181 "log(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
182 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
183 "Return the natural logarithm of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
184
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
185
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
186 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
187 c_log10(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
189 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 double l = hypot(x.real,x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 r.imag = atan2(x.imag, x.real)/log(10.);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 r.real = log10(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
196 static char c_log10_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
197 "log10(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
198 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
199 "Return the base-10 logarithm of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
200
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
201
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
202 /* internal function not available from Python */
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
203 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
204 c_prodi(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
206 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 r.real = -x.imag;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 r.imag = x.real;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
212
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
213 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
214 c_sin(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
216 Py_complex r;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
217 r.real = sin(x.real) * cosh(x.imag);
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
218 r.imag = cos(x.real) * sinh(x.imag);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
222 static char c_sin_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
223 "sin(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
224 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
225 "Return the sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
226
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
227
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
228 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
229 c_sinh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
231 Py_complex r;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
232 r.real = cos(x.imag) * sinh(x.real);
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
233 r.imag = sin(x.imag) * cosh(x.real);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
237 static char c_sinh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
238 "sinh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
239 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
240 "Return the hyperbolic sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
241
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
242
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
243 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
244 c_sqrt(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
246 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 double s,d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 if (x.real == 0. && x.imag == 0.)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 r = x;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 else {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 d = 0.5*x.imag/s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 if (x.real > 0.) {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 r.real = s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 r.imag = d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 else if (x.imag >= 0.) {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 r.real = d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 r.imag = s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 else {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 r.real = -d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 r.imag = -s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
269 static char c_sqrt_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
270 "sqrt(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
271 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
272 "Return the square root of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
273
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
274
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
275 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
276 c_tan(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
278 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 double sr,cr,shi,chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 sr = sin(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 cr = cos(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 shi = sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 chi = cosh(x.imag);
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
286 rs = sr * chi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
287 is = cr * shi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
288 rc = cr * chi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
289 ic = -sr * shi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
290 d = rc*rc + ic * ic;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
291 r.real = (rs*rc + is*ic) / d;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
292 r.imag = (is*rc - rs*ic) / d;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
295
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
296 static char c_tan_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
297 "tan(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
298 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
299 "Return the tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
300
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
301
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
302 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
303 c_tanh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
305 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 double si,ci,shr,chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 si = sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 ci = cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 shr = sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 chr = cosh(x.real);
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
313 rs = ci * shr;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
314 is = si * chr;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
315 rc = ci * chr;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
316 ic = si * shr;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 d = rc*rc + ic*ic;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
318 r.real = (rs*rc + is*ic) / d;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
319 r.imag = (is*rc - rs*ic) / d;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
323 static char c_tanh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
324 "tanh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
325 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
326 "Return the hyperbolic tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
327
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
328
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 /* And now the glue to make them available from Python: */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
331 static PyObject *
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13493
diff changeset
332 math_error(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 if (errno == EDOM)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
335 PyErr_SetString(PyExc_ValueError, "math domain error");
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 else if (errno == ERANGE)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
337 PyErr_SetString(PyExc_OverflowError, "math range error");
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
338 else /* Unexpected math error */
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
339 PyErr_SetFromErrno(PyExc_ValueError);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
343 static PyObject *
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
344 math_1(PyObject *args, Py_complex (*func)(Py_complex))
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
346 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 if (!PyArg_ParseTuple(args, "D", &x))
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4649
diff changeset
350 PyFPE_START_PROTECT("complex function", return 0)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
352 PyFPE_END_PROTECT(x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 CHECK(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 CHECK(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 if (errno != 0)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 return math_error();
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 else
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
358 return PyComplex_FromCComplex(x);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 #define FUNC1(stubname, func) \
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
362 static PyObject * stubname(PyObject *self, PyObject *args) { \
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 return math_1(args, func); \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
365
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 FUNC1(cmath_acos, c_acos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 FUNC1(cmath_acosh, c_acosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 FUNC1(cmath_asin, c_asin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 FUNC1(cmath_asinh, c_asinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 FUNC1(cmath_atan, c_atan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 FUNC1(cmath_atanh, c_atanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 FUNC1(cmath_cos, c_cos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 FUNC1(cmath_cosh, c_cosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 FUNC1(cmath_exp, c_exp)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 FUNC1(cmath_log, c_log)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 FUNC1(cmath_log10, c_log10)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 FUNC1(cmath_sin, c_sin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 FUNC1(cmath_sinh, c_sinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 FUNC1(cmath_sqrt, c_sqrt)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 FUNC1(cmath_tan, c_tan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 FUNC1(cmath_tanh, c_tanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
384 static char module_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
385 "This module is always available. It provides access to mathematical\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
386 "functions for complex numbers.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
387
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
388 static PyMethodDef cmath_methods[] = {
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
389 {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
390 {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
391 {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
392 {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
393 {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
394 {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
395 {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
396 {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
397 {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
398 {"log", cmath_log, METH_VARARGS, c_log_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
399 {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
400 {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
401 {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
402 {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
403 {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
404 {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 {NULL, NULL} /* sentinel */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 };
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
408 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13493
diff changeset
409 initcmath(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 {
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
411 PyObject *m, *d, *v;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
412
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
413 m = Py_InitModule3("cmath", cmath_methods, module_doc);
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
414 d = PyModule_GetDict(m);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
415 PyDict_SetItemString(d, "pi",
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
416 v = PyFloat_FromDouble(atan(1.0) * 4.0));
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
417 Py_DECREF(v);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
418 PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
419 Py_DECREF(v);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 }