summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-10-20 20:16:45 (GMT)
committerGuido van Rossum <guido@python.org>1991-10-20 20:16:45 (GMT)
commit15ecff4c5e98557127c4b99969531d110fc18c1a (patch)
tree66b5dd17680e9e30d5882897e23ba3308172b2bf /Objects
parent5063bab9735a7b2d91ad58820c51455f87a9bea1 (diff)
downloadcpython-15ecff4c5e98557127c4b99969531d110fc18c1a.zip
cpython-15ecff4c5e98557127c4b99969531d110fc18c1a.tar.gz
cpython-15ecff4c5e98557127c4b99969531d110fc18c1a.tar.bz2
Finally implemented divmod().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/floatobject.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index a386e0c..d89e1cb 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -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 *