Properly(?) implemented remainder and divmod (Tim Hochberg)

This commit is contained in:
Guido van Rossum 1996-09-12 20:56:18 +00:00
parent 934a4cea85
commit 3be12e97cd
1 changed files with 34 additions and 5 deletions

View File

@ -364,7 +364,7 @@ complex_div(v, w)
c_error = 0; c_error = 0;
quot = c_quot(v->cval,w->cval); quot = c_quot(v->cval,w->cval);
if (c_error == 1) { if (c_error == 1) {
err_setstr(ZeroDivisionError, "float division"); err_setstr(ZeroDivisionError, "complex division");
return NULL; return NULL;
} }
return newcomplexobject(quot); return newcomplexobject(quot);
@ -375,14 +375,43 @@ complex_remainder(v, w)
complexobject *v; complexobject *v;
complexobject *w; complexobject *w;
{ {
err_setstr(TypeError, Py_complex div, mod;
"remainder and divmod not implemented for complex numbers"); div = c_quot(v->cval,w->cval); /* The raw divisor value. */
if (c_error == 1) {
err_setstr(ZeroDivisionError, "complex remainder");
return NULL; return NULL;
} }
div.real = floor(div.real); /* Use the floor of the real part. */
div.imag = 0.0;
mod = c_diff(v->cval, c_prod(w->cval, div));
#define complex_divmod complex_remainder return newcomplexobject(mod);
}
static object *
complex_divmod(v, w)
complexobject *v;
complexobject *w;
{
Py_complex div, mod;
PyObject *d, *m, *z;
div = c_quot(v->cval,w->cval); /* The raw divisor value. */
if (c_error == 1) {
err_setstr(ZeroDivisionError, "complex divmod()");
return NULL;
}
div.real = floor(div.real); /* Use the floor of the real part. */
div.imag = 0.0;
mod = c_diff(v->cval, c_prod(w->cval, div));
d = newcomplexobject(div);
m = newcomplexobject(mod);
z = mkvalue("(OO)", d, m);
Py_XDECREF(d);
Py_XDECREF(m);
return z;
}
static object * static object *
complex_pow(v, w, z) complex_pow(v, w, z)
complexobject *v; complexobject *v;