comparison Python/bltinmodule.c @ 3323:6098aaa8ae7f legacy-trunk

changes for complex numbers
author Guido van Rossum <guido@python.org>
date Fri, 12 Jan 1996 01:09:56 +0000
parents 04e1690fb4d1
children f8ff28329971
comparison
equal deleted inserted replaced
3322:596347db6a68 3323:6098aaa8ae7f
278 "compile() mode must be 'exec' or 'eval' or 'single'"); 278 "compile() mode must be 'exec' or 'eval' or 'single'");
279 return NULL; 279 return NULL;
280 } 280 }
281 return compile_string(str, filename, start); 281 return compile_string(str, filename, start);
282 } 282 }
283
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
283 328
284 static object * 329 static object *
285 builtin_dir(self, args) 330 builtin_dir(self, args)
286 object *self; 331 object *self;
287 object *args; 332 object *args;
952 if (v->ob_type->tp_as_number == NULL || 997 if (v->ob_type->tp_as_number == NULL ||
953 w->ob_type->tp_as_number == NULL) { 998 w->ob_type->tp_as_number == NULL) {
954 err_setstr(TypeError, "pow() requires numeric arguments"); 999 err_setstr(TypeError, "pow() requires numeric arguments");
955 return NULL; 1000 return NULL;
956 } 1001 }
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) {
958 if (!err_occurred()) 1007 if (!err_occurred())
959 err_setstr(ValueError, "negative number to float power"); 1008 err_setstr(ValueError, "negative number to float power");
960 return NULL; 1009 return NULL;
961 } 1010 }
962 if (coerce(&v, &w) != 0) 1011 if (coerce(&v, &w) != 0)
1392 {"callable", builtin_callable, 1}, 1441 {"callable", builtin_callable, 1},
1393 {"chr", builtin_chr, 1}, 1442 {"chr", builtin_chr, 1},
1394 {"cmp", builtin_cmp, 1}, 1443 {"cmp", builtin_cmp, 1},
1395 {"coerce", builtin_coerce, 1}, 1444 {"coerce", builtin_coerce, 1},
1396 {"compile", builtin_compile, 1}, 1445 {"compile", builtin_compile, 1},
1446 #ifndef WITHOUT_COMPLEX
1447 {"complex", builtin_complex, 1},
1448 #endif
1397 {"delattr", builtin_delattr, 1}, 1449 {"delattr", builtin_delattr, 1},
1398 {"dir", builtin_dir, 1}, 1450 {"dir", builtin_dir, 1},
1399 {"divmod", builtin_divmod, 1}, 1451 {"divmod", builtin_divmod, 1},
1400 {"eval", builtin_eval, 1}, 1452 {"eval", builtin_eval, 1},
1401 {"execfile", builtin_execfile, 1}, 1453 {"execfile", builtin_execfile, 1},