]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Finally implemented divmod().
authorGuido van Rossum <guido@python.org>
Sun, 20 Oct 1991 20:16:45 +0000 (20:16 +0000)
committerGuido van Rossum <guido@python.org>
Sun, 20 Oct 1991 20:16:45 +0000 (20:16 +0000)
Objects/floatobject.c

index a386e0c2f15eacacdbaa8168097766a1d0690fa9..d89e1cb334cefb0bc8bb63802651f648e98ff54a 100644 (file)
@@ -203,13 +203,35 @@ 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");
-       return NULL;
+       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 *