annotate Modules/cmathmodule.c @ 15164:8930ec3bc00a legacy-trunk

Added additional test cases for pulldom modifications.
author Lars Gustäbel <lars@gustaebel.de>
date Wed, 11 Oct 2000 22:36:00 +0000
parents 3c0f75a2198f
children 9696c95d09b7
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 */
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
27 static Py_complex c_1 = {1., 0.};
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.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
30 static Py_complex c_i2 = {0., 0.5};
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 3771
diff changeset
31 #if 0
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
32 static Py_complex c_mi = {0., -1.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
33 static Py_complex c_pi2 = {M_PI/2., 0.};
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 3771
diff changeset
34 #endif
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 /* forward declarations */
13685
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
37 staticforward Py_complex c_log(Py_complex);
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
38 staticforward Py_complex c_prodi(Py_complex);
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
39 staticforward Py_complex c_sqrt(Py_complex);
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
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
42 static Py_complex c_acos(Py_complex 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 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 c_sqrt(c_diff(c_1,c_prod(x,x))))))));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
48 static char c_acos_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
49 "acos(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
50 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
51 Return the arc cosine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
52
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
53
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
54 static Py_complex c_acosh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 {
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
56 Py_complex z;
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
57 z = c_sqrt(c_half);
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
58 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_1)),
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
59 c_sqrt(c_diff(x,c_1)))));
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
60 return c_sum(z, z);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
63 static char c_acosh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
64 "acosh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
65 \n\
10403
9c6da30ae675 Docstring fix: acosh() returns the hyperbolic arccosine, not the
Fred Drake <fdrake@acm.org>
parents: 9886
diff changeset
66 Return the hyperbolic arccosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
67
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
68
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
69 static Py_complex c_asin(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 {
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
71 Py_complex z;
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
72 z = c_sqrt(c_half);
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
73 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_i)),
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
74 c_sqrt(c_diff(x,c_i)))));
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
75 return c_sum(z, z);
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
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
78 static char c_asin_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
79 "asin(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
80 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
81 Return the arc sine of x.";
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
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
84 static Py_complex c_asinh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 {
9886
fae6d24b897b Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
86 /* Break up long expression for WATCOM */
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;
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
88 z = c_sum(c_1,c_prod(x, x));
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
89 return c_log(c_sum(c_sqrt(z), x));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
92 static char c_asinh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
93 "asinh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
94 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
95 Return the hyperbolic arc sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
96
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
97
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
98 static Py_complex c_atan(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
103 static char c_atan_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
104 "atan(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
105 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
106 Return the arc tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
107
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
108
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
109 static Py_complex c_atanh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
114 static char c_atanh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
115 "atanh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
116 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
117 Return the hyperbolic arc tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
118
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
119
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
120 static Py_complex c_cos(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
122 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 r.real = cos(x.real)*cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 r.imag = -sin(x.real)*sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
128 static char c_cos_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
129 "cos(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
130 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
131 Return the cosine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
132
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
133
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
134 static Py_complex c_cosh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
136 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 r.real = cos(x.imag)*cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 r.imag = sin(x.imag)*sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
142 static char c_cosh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
143 "cosh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
144 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
145 Return the hyperbolic cosine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
146
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
147
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
148 static Py_complex c_exp(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
150 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 double l = exp(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 r.real = l*cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 r.imag = l*sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
157 static char c_exp_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
158 "exp(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
159 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
160 Return the exponential value e**x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
161
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
162
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
163 static Py_complex c_log(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
165 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 double l = hypot(x.real,x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 r.imag = atan2(x.imag, x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 r.real = log(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
172 static char c_log_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
173 "log(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
174 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
175 Return the natural logarithm of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
176
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
177
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
178 static Py_complex c_log10(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
180 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 double l = hypot(x.real,x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 r.imag = atan2(x.imag, x.real)/log(10.);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 r.real = log10(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
187 static char c_log10_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
188 "log10(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
189 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
190 Return the base-10 logarithm of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
191
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
192
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
193 /* internal function not available from Python */
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
194 static Py_complex c_prodi(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
196 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 r.real = -x.imag;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 r.imag = x.real;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
202
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
203 static Py_complex c_sin(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
205 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 r.real = sin(x.real)*cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 r.imag = cos(x.real)*sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
211 static char c_sin_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
212 "sin(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
213 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
214 Return the sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
215
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
216
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
217 static Py_complex c_sinh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
219 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 r.real = cos(x.imag)*sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 r.imag = sin(x.imag)*cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
225 static char c_sinh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
226 "sinh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
227 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
228 Return the hyperbolic sine of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
229
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
230
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
231 static Py_complex c_sqrt(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
233 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 double s,d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 if (x.real == 0. && x.imag == 0.)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 r = x;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 else {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 d = 0.5*x.imag/s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 if (x.real > 0.) {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 r.real = s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 r.imag = d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 else if (x.imag >= 0.) {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 r.real = d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 r.imag = s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 else {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 r.real = -d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 r.imag = -s;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
256 static char c_sqrt_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
257 "sqrt(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
258 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
259 Return the square root of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
260
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
261
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
262 static Py_complex c_tan(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
264 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 double sr,cr,shi,chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 sr = sin(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 cr = cos(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 shi = sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 chi = cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 rs = sr*chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 is = cr*shi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 rc = cr*chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 ic = -sr*shi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 d = rc*rc + ic*ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 r.real = (rs*rc+is*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 r.imag = (is*rc-rs*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
281
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
282 static char c_tan_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
283 "tan(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
284 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
285 Return the tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
286
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
287
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
288 static Py_complex c_tanh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
290 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 double si,ci,shr,chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 si = sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 ci = cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 shr = sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 chr = cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 rs = ci*shr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 is = si*chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 rc = ci*chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 ic = si*shr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 d = rc*rc + ic*ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 r.real = (rs*rc+is*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 r.imag = (is*rc-rs*ic)/d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
307
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
308 static char c_tanh_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
309 "tanh(x)\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
310 \n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
311 Return the hyperbolic tangent of x.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
312
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 /* And now the glue to make them available from Python: */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
316 static PyObject *
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13493
diff changeset
317 math_error(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 if (errno == EDOM)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
320 PyErr_SetString(PyExc_ValueError, "math domain error");
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 else if (errno == ERANGE)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
322 PyErr_SetString(PyExc_OverflowError, "math range error");
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
323 else /* Unexpected math error */
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
324 PyErr_SetFromErrno(PyExc_ValueError);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
328 static PyObject *
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
329 math_1(PyObject *args, Py_complex (*func)(Py_complex))
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
331 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 if (!PyArg_ParseTuple(args, "D", &x))
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4649
diff changeset
335 PyFPE_START_PROTECT("complex function", return 0)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
337 PyFPE_END_PROTECT(x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 CHECK(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 CHECK(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 if (errno != 0)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 return math_error();
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 else
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
343 return PyComplex_FromCComplex(x);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 #define FUNC1(stubname, func) \
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
347 static PyObject * stubname(PyObject *self, PyObject *args) { \
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 return math_1(args, func); \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 FUNC1(cmath_acos, c_acos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 FUNC1(cmath_acosh, c_acosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 FUNC1(cmath_asin, c_asin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 FUNC1(cmath_asinh, c_asinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 FUNC1(cmath_atan, c_atan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 FUNC1(cmath_atanh, c_atanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 FUNC1(cmath_cos, c_cos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 FUNC1(cmath_cosh, c_cosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 FUNC1(cmath_exp, c_exp)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 FUNC1(cmath_log, c_log)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 FUNC1(cmath_log10, c_log10)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 FUNC1(cmath_sin, c_sin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 FUNC1(cmath_sinh, c_sinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 FUNC1(cmath_sqrt, c_sqrt)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 FUNC1(cmath_tan, c_tan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 FUNC1(cmath_tanh, c_tanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
369 static char module_doc [] =
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
370 "This module is always available. It provides access to mathematical\n\
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
371 functions for complex numbers.";
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
372
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
373
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
374 static PyMethodDef cmath_methods[] = {
13817
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
375 {"acos", cmath_acos,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
376 METH_VARARGS, c_acos_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
377 {"acosh", cmath_acosh,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
378 METH_VARARGS, c_acosh_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
379 {"asin", cmath_asin,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
380 METH_VARARGS, c_asin_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
381 {"asinh", cmath_asinh,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
382 METH_VARARGS, c_asinh_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
383 {"atan", cmath_atan,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
384 METH_VARARGS, c_atan_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
385 {"atanh", cmath_atanh,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
386 METH_VARARGS, c_atanh_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
387 {"cos", cmath_cos,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
388 METH_VARARGS, c_cos_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
389 {"cosh", cmath_cosh,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
390 METH_VARARGS, c_cosh_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
391 {"exp", cmath_exp,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
392 METH_VARARGS, c_exp_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
393 {"log", cmath_log,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
394 METH_VARARGS, c_log_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
395 {"log10", cmath_log10,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
396 METH_VARARGS, c_log10_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
397 {"sin", cmath_sin,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
398 METH_VARARGS, c_sin_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
399 {"sinh", cmath_sinh,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
400 METH_VARARGS, c_sinh_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
401 {"sqrt", cmath_sqrt,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
402 METH_VARARGS, c_sqrt_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
403 {"tan", cmath_tan,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
404 METH_VARARGS, c_tan_doc},
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
405 {"tanh", cmath_tanh,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13761
diff changeset
406 METH_VARARGS, c_tanh_doc},
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 {NULL, NULL} /* sentinel */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 };
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
410 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13493
diff changeset
411 initcmath(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 {
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
413 PyObject *m, *d, *v;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
415 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
416 d = PyModule_GetDict(m);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
417 PyDict_SetItemString(d, "pi",
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
418 v = PyFloat_FromDouble(atan(1.0) * 4.0));
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
419 Py_DECREF(v);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
420 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
421 Py_DECREF(v);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 }