Finally implemented divmod().

This commit is contained in:
Guido van Rossum 1991-10-20 20:16:45 +00:00
parent 5063bab973
commit 15ecff4c5e
1 changed files with 25 additions and 3 deletions

View File

@ -203,14 +203,36 @@ float_divmod(v, w)
floatobject *v;
object *w;
{
double wx;
double vx, wx;
double div, mod;
object *t;
if (!is_floatobject(w)) {
err_badarg();
return NULL;
}
err_setstr(RuntimeError, "divmod() on float not implemented");
wx = ((floatobject *)w) -> ob_fval;
if (wx == 0.0) {
err_setstr(ZeroDivisionError, "float division by zero");
return NULL;
}
vx = v->ob_fval;
mod = fmod(vx, wx);
div = (vx - mod) / wx;
if (wx*mod < 0) {
mod += wx;
div -= 1.0;
}
t = newtupleobject(2);
if (t != NULL) {
settupleitem(t, 0, newfloatobject(div));
settupleitem(t, 1, newfloatobject(mod));
if (err_occurred()) {
DECREF(t);
t = NULL;
}
}
return t;
}
static object *
float_pow(v, w)