annotate Modules/cmathmodule.c @ 20737:00a581bdedeb legacy-trunk

When overriding __str__ or __repr__, set the tp_print slot to NULL.
author Guido van Rossum <guido@python.org>
date Mon, 29 Oct 2001 14:33:44 +0000
parents 64bcef1702ad
children 2f48ae5c8a94
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 #ifndef M_PI
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #define M_PI (3.141592653589793239)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #endif
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 /* First, the C functions that do the real work */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 /* constants */
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
14 static Py_complex c_one = {1., 0.};
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
15 static Py_complex c_half = {0.5, 0.};
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
16 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
17 static Py_complex c_halfi = {0., 0.5};
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 /* forward declarations */
13685
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
20 staticforward Py_complex c_log(Py_complex);
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
21 staticforward Py_complex c_prodi(Py_complex);
129c9775cc19 Fix typo in previous patch.
Thomas Wouters <thomas@python.org>
parents: 13684
diff changeset
22 staticforward Py_complex c_sqrt(Py_complex);
3334
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
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
25 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
26 c_acos(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 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
29 c_sqrt(c_diff(c_one,c_prod(x,x))))))));
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
32 static char c_acos_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
33 "acos(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
34 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
35 "Return the arc cosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
36
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
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_acosh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 {
13169
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
41 Py_complex z;
b78b996e8ebc Patch by Nadav Horesh to make acosh and asinh better.
Guido van Rossum <guido@python.org>
parents: 10403
diff changeset
42 z = c_sqrt(c_half);
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
43 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
44 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
45 return c_sum(z, z);
3334
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
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
48 static char c_acosh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
49 "acosh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
50 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
51 "Return the hyperbolic arccosine of x.";
9593
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
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
54 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
55 c_asin(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 {
16722
cc338a5c16e5 Bug #133297: cmath.asin is the same as cmath.asinh.
Tim Peters <tim.peters@gmail.com>
parents: 16708
diff changeset
57 /* -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
58 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
59 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
60 return c_neg(c_prodi(c_log(
844fde600a61 asin micro-optimization suggested in email.
Tim Peters <tim.peters@gmail.com>
parents: 16722
diff changeset
61 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
62 ) ) );
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
65 static char c_asin_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
66 "asin(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
67 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
68 "Return the arc sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
69
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
70
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
71 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
72 c_asinh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 {
9886
fae6d24b897b Jim Ahlstrom patch: Watcom chokes on a long expression in c_asinh().
Guido van Rossum <guido@python.org>
parents: 9593
diff changeset
74 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
75 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
76 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
77 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
78 return c_sum(z, z);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
81 static char c_asinh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
82 "asinh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
83 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
84 "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
85
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
86
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
87 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
88 c_atan(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 {
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
90 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
91 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
93 static char c_atan_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
94 "atan(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
95 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
96 "Return the arc tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
97
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
98
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
99 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
100 c_atanh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 {
16708
57799f013aad Rename some constants for easier readability.
Tim Peters <tim.peters@gmail.com>
parents: 16707
diff changeset
102 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
103 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
105 static char c_atanh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
106 "atanh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
107 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
108 "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
109
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
110
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
111 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
112 c_cos(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
114 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 r.real = cos(x.real)*cosh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 r.imag = -sin(x.real)*sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
120 static char c_cos_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
121 "cos(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
122 "n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
123 "Return the cosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
124
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
125
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
126 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
127 c_cosh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
129 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 r.real = cos(x.imag)*cosh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 r.imag = sin(x.imag)*sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
135 static char c_cosh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
136 "cosh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
137 "n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
138 "Return the hyperbolic cosine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
139
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
140
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
141 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
142 c_exp(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
144 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 double l = exp(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 r.real = l*cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 r.imag = l*sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
151 static char c_exp_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
152 "exp(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
153 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
154 "Return the exponential value e**x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
155
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
156
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
157 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
158 c_log(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
160 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 double l = hypot(x.real,x.imag);
19758
64bcef1702ad Revert parts of patch #453627, documenting the resulting test failures
Martin v. Löwis <martin@v.loewis.de>
parents: 19750
diff changeset
162 r.imag = atan2(x.imag, x.real);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 r.real = log(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
167 static char c_log_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
168 "log(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
169 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
170 "Return the natural logarithm of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
171
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
172
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
173 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
174 c_log10(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
176 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 double l = hypot(x.real,x.imag);
19758
64bcef1702ad Revert parts of patch #453627, documenting the resulting test failures
Martin v. Löwis <martin@v.loewis.de>
parents: 19750
diff changeset
178 r.imag = atan2(x.imag, x.real)/log(10.);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 r.real = log10(l);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
183 static char c_log10_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
184 "log10(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
185 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
186 "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
187
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
188
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
189 /* 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
190 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
191 c_prodi(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
193 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 r.real = -x.imag;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 r.imag = x.real;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
199
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
200 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
201 c_sin(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
203 Py_complex r;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
204 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
205 r.imag = cos(x.real) * sinh(x.imag);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
209 static char c_sin_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
210 "sin(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
211 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
212 "Return the sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
213
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
214
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
215 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
216 c_sinh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
218 Py_complex r;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
219 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
220 r.imag = sin(x.imag) * cosh(x.real);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
224 static char c_sinh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
225 "sinh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
226 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
227 "Return the hyperbolic sine of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
228
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
229
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
230 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
231 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
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
256 static char c_sqrt_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
257 "sqrt(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
258 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
259 "Return the square root of x.";
9593
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
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
262 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
263 c_tan(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
265 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 double sr,cr,shi,chi;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 sr = sin(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 cr = cos(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 shi = sinh(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 chi = cosh(x.imag);
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
273 rs = sr * chi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
274 is = cr * shi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
275 rc = cr * chi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
276 ic = -sr * shi;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
277 d = rc*rc + ic * ic;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
278 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
279 r.imag = (is*rc - rs*ic) / d;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
283 static char c_tan_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
284 "tan(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
285 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
286 "Return the tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
287
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
288
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
289 static Py_complex
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
290 c_tanh(Py_complex x)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
292 Py_complex r;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 double si,ci,shr,chr;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 double rs,is,rc,ic;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 double d;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 si = sin(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 ci = cos(x.imag);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 shr = sinh(x.real);
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 chr = cosh(x.real);
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
300 rs = ci * shr;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
301 is = si * chr;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
302 rc = ci * chr;
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
303 ic = si * shr;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 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
305 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
306 r.imag = (is*rc - rs*ic) / d;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 return r;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
310 static char c_tanh_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
311 "tanh(x)\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
312 "\n"
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
313 "Return the hyperbolic tangent of x.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
314
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 /* And now the glue to make them available from Python: */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
318 static PyObject *
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13493
diff changeset
319 math_error(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 {
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 if (errno == EDOM)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
322 PyErr_SetString(PyExc_ValueError, "math domain error");
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 else if (errno == ERANGE)
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
324 PyErr_SetString(PyExc_OverflowError, "math range error");
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
325 else /* Unexpected math error */
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
326 PyErr_SetFromErrno(PyExc_ValueError);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 return NULL;
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
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
330 static PyObject *
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
331 math_1(PyObject *args, Py_complex (*func)(Py_complex))
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 {
3771
d0988318b756 complex -> Py_complex
Guido van Rossum <guido@python.org>
parents: 3615
diff changeset
333 Py_complex x;
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 if (!PyArg_ParseTuple(args, "D", &x))
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 return NULL;
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4649
diff changeset
337 PyFPE_START_PROTECT("complex function", return 0)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 x = (*func)(x);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
339 PyFPE_END_PROTECT(x)
19750
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
340 Py_SET_ERANGE_IF_OVERFLOW(x.real);
35b6a1d0b1e0 Rework the way we try to check for libm overflow, given that C99 no longer
Tim Peters <tim.peters@gmail.com>
parents: 19730
diff changeset
341 Py_SET_ERANGE_IF_OVERFLOW(x.imag);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 if (errno != 0)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 return math_error();
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 else
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
345 return PyComplex_FromCComplex(x);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 #define FUNC1(stubname, func) \
13493
8bacf8631a11 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
349 static PyObject * stubname(PyObject *self, PyObject *args) { \
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 return math_1(args, func); \
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 }
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 FUNC1(cmath_acos, c_acos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 FUNC1(cmath_acosh, c_acosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 FUNC1(cmath_asin, c_asin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 FUNC1(cmath_asinh, c_asinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 FUNC1(cmath_atan, c_atan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 FUNC1(cmath_atanh, c_atanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 FUNC1(cmath_cos, c_cos)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 FUNC1(cmath_cosh, c_cosh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 FUNC1(cmath_exp, c_exp)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 FUNC1(cmath_log, c_log)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 FUNC1(cmath_log10, c_log10)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 FUNC1(cmath_sin, c_sin)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 FUNC1(cmath_sinh, c_sinh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 FUNC1(cmath_sqrt, c_sqrt)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 FUNC1(cmath_tan, c_tan)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 FUNC1(cmath_tanh, c_tanh)
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
371 static char module_doc[] =
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
372 "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
373 "functions for complex numbers.";
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
374
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
375 static PyMethodDef cmath_methods[] = {
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
376 {"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
377 {"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
378 {"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
379 {"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
380 {"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
381 {"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
382 {"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
383 {"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
384 {"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
385 {"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
386 {"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
387 {"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
388 {"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
389 {"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
390 {"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
391 {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 {NULL, NULL} /* sentinel */
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 };
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
395 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13493
diff changeset
396 initcmath(void)
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 {
4639
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
398 PyObject *m, *d, *v;
16707
9696c95d09b7 Mechanical edits just so I can read it.
Tim Peters <tim.peters@gmail.com>
parents: 13817
diff changeset
399
9593
f22f769bf3d9 Added Doc strings -- by Chris Petrilli.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
400 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
401 d = PyModule_GetDict(m);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
402 PyDict_SetItemString(d, "pi",
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
403 v = PyFloat_FromDouble(atan(1.0) * 4.0));
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
404 Py_DECREF(v);
97ec9dfbb2b6 Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
405 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
406 Py_DECREF(v);
3334
969bd911e114 complex math
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 }