Skip to content

Commit 8a5c5d2

Browse files
committed
changes for complex numbers
1 parent 0bfd6c3 commit 8a5c5d2

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

Python/bltinmodule.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,51 @@ builtin_compile(self, args)
281281
return compile_string(str, filename, start);
282282
}
283283

284+
#ifndef WITHOUT_COMPLEX
285+
286+
static object *
287+
builtin_complex(self, args)
288+
object *self;
289+
object *args;
290+
{
291+
object *r, *i;
292+
number_methods *nbr, *nbi;
293+
complex cr, ci;
294+
295+
i = NULL;
296+
if (!newgetargs(args, "O|O:complex", &r, &i))
297+
return NULL;
298+
if ((nbr = r->ob_type->tp_as_number) == NULL ||
299+
nbr->nb_float == NULL || (i != NULL &&
300+
((nbi = i->ob_type->tp_as_number) == NULL ||
301+
nbi->nb_float == NULL))) {
302+
err_setstr(TypeError,
303+
"complex() argument can't be converted to complex");
304+
return NULL;
305+
}
306+
if (is_complexobject(r))
307+
cr = ((complexobject*)r)->cval;
308+
else {
309+
cr.real = getfloatvalue((*nbr->nb_float)(r));
310+
cr.imag = 0.;
311+
}
312+
if (i == NULL) {
313+
ci.real = 0.;
314+
ci.imag = 0.;
315+
}
316+
else if (is_complexobject(i))
317+
ci = ((complexobject*)i)->cval;
318+
else {
319+
ci.real = getfloatvalue((*nbi->nb_float)(i));
320+
ci.imag = 0.;
321+
}
322+
cr.real -= ci.imag;
323+
cr.imag += ci.real;
324+
return newcomplexobject(cr);
325+
}
326+
327+
#endif
328+
284329
static object *
285330
builtin_dir(self, args)
286331
object *self;
@@ -954,7 +999,11 @@ do_pow(v, w)
954999
err_setstr(TypeError, "pow() requires numeric arguments");
9551000
return NULL;
9561001
}
957-
if (is_floatobject(w) && getfloatvalue(v) < 0.0) {
1002+
if (
1003+
#ifndef WITHOUT_COMPLEX
1004+
!is_complexobject(v) &&
1005+
#endif
1006+
is_floatobject(w) && getfloatvalue(v) < 0.0) {
9581007
if (!err_occurred())
9591008
err_setstr(ValueError, "negative number to float power");
9601009
return NULL;
@@ -1394,6 +1443,9 @@ static struct methodlist builtin_methods[] = {
13941443
{"cmp", builtin_cmp, 1},
13951444
{"coerce", builtin_coerce, 1},
13961445
{"compile", builtin_compile, 1},
1446+
#ifndef WITHOUT_COMPLEX
1447+
{"complex", builtin_complex, 1},
1448+
#endif
13971449
{"delattr", builtin_delattr, 1},
13981450
{"dir", builtin_dir, 1},
13991451
{"divmod", builtin_divmod, 1},

Python/getargs.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,17 @@ convertsimple1(arg, p_format, p_va)
498498
break;
499499
}
500500

501+
case 'D': /* complex double */
502+
{
503+
complex *p = va_arg(*p_va, complex *);
504+
complex cval = PyComplex_AsCComplex(arg);
505+
if (err_occurred())
506+
return "complex<D>";
507+
else
508+
*p = cval;
509+
break;
510+
}
511+
501512
case 'c': /* char */
502513
{
503514
char *p = va_arg(*p_va, char *);

Python/marshal.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3939
#define TYPE_NONE 'N'
4040
#define TYPE_INT 'i'
4141
#define TYPE_FLOAT 'f'
42+
#define TYPE_COMPLEX 'x'
4243
#define TYPE_LONG 'l'
4344
#define TYPE_STRING 's'
4445
#define TYPE_TUPLE '('
@@ -150,6 +151,26 @@ w_object(v, p)
150151
w_byte(n, p);
151152
w_string(buf, n, p);
152153
}
154+
#ifndef WITHOUT_COMPLEX
155+
else if (is_complexobject(v)) {
156+
extern void float_buf_repr PROTO((char *, floatobject *));
157+
char buf[256]; /* Plenty to format any double */
158+
floatobject *temp;
159+
w_byte(TYPE_COMPLEX, p);
160+
temp = (floatobject*)newfloatobject(PyComplex_RealAsDouble(v));
161+
float_buf_repr(buf, temp);
162+
DECREF(temp);
163+
n = strlen(buf);
164+
w_byte(n, p);
165+
w_string(buf, n, p);
166+
temp = (floatobject*)newfloatobject(PyComplex_ImagAsDouble(v));
167+
float_buf_repr(buf, temp);
168+
DECREF(temp);
169+
n = strlen(buf);
170+
w_byte(n, p);
171+
w_string(buf, n, p);
172+
}
173+
#endif
153174
else if (is_stringobject(v)) {
154175
w_byte(TYPE_STRING, p);
155176
n = getstringsize(v);
@@ -329,6 +350,32 @@ r_object(p)
329350
return newfloatobject(atof(buf));
330351
}
331352

353+
#ifndef WITHOUT_COMPLEX
354+
case TYPE_COMPLEX:
355+
{
356+
extern double atof PROTO((const char *));
357+
char buf[256];
358+
complex c;
359+
n = r_byte(p);
360+
if (r_string(buf, (int)n, p) != n) {
361+
err_setstr(EOFError,
362+
"EOF read where object expected");
363+
return NULL;
364+
}
365+
buf[n] = '\0';
366+
c.real = atof(buf);
367+
n = r_byte(p);
368+
if (r_string(buf, (int)n, p) != n) {
369+
err_setstr(EOFError,
370+
"EOF read where object expected");
371+
return NULL;
372+
}
373+
buf[n] = '\0';
374+
c.imag = atof(buf);
375+
return newcomplexobject(c);
376+
}
377+
#endif
378+
332379
case TYPE_STRING:
333380
n = r_long(p);
334381
v = newsizedstringobject((char *)NULL, n);

0 commit comments

Comments
 (0)