diff Objects/floatobject.c @ 632:0c1938f2e0c0 legacy-trunk

Get rid of redundant type checks. Define % operator similar to int%int.
author Guido van Rossum <guido@python.org>
date Sun, 26 Jan 1992 18:16:35 +0000
parents 0ac392b7f76e
children c7b23cc8cded
line wrap: on
line diff
--- a/Objects/floatobject.c	Sun Jan 26 18:15:48 1992 +0000
+++ b/Objects/floatobject.c	Sun Jan 26 18:16:35 1992 +0000
@@ -139,86 +139,70 @@
 static object *
 float_add(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
-	if (!is_floatobject(w)) {
-		err_badarg();
-		return NULL;
-	}
-	return newfloatobject(v->ob_fval + ((floatobject *)w) -> ob_fval);
+	return newfloatobject(v->ob_fval + w->ob_fval);
 }
 
 static object *
 float_sub(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
-	if (!is_floatobject(w)) {
-		err_badarg();
-		return NULL;
-	}
-	return newfloatobject(v->ob_fval - ((floatobject *)w) -> ob_fval);
+	return newfloatobject(v->ob_fval - w->ob_fval);
 }
 
 static object *
 float_mul(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
-	if (!is_floatobject(w)) {
-		err_badarg();
-		return NULL;
-	}
-	return newfloatobject(v->ob_fval * ((floatobject *)w) -> ob_fval);
+	return newfloatobject(v->ob_fval * w->ob_fval);
 }
 
 static object *
 float_div(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
-	if (!is_floatobject(w)) {
-		err_badarg();
-		return NULL;
-	}
-	if (((floatobject *)w) -> ob_fval == 0) {
+	if (w->ob_fval == 0) {
 		err_setstr(ZeroDivisionError, "float division");
 		return NULL;
 	}
-	return newfloatobject(v->ob_fval / ((floatobject *)w) -> ob_fval);
+	return newfloatobject(v->ob_fval / w->ob_fval);
 }
 
 static object *
 float_rem(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
-	double wx;
-	if (!is_floatobject(w)) {
-		err_badarg();
+	double vx, wx;
+	double div, mod;
+	wx = w->ob_fval;
+	if (wx == 0.0) {
+		err_setstr(ZeroDivisionError, "float modulo");
 		return NULL;
 	}
-	wx = ((floatobject *)w) -> ob_fval;
-	if (wx == 0.0) {
-		err_setstr(ZeroDivisionError, "float remainder");
-		return NULL;
+	vx = v->ob_fval;
+	mod = fmod(vx, wx);
+	div = (vx - mod) / wx;
+	if (wx*mod < 0) {
+		mod += wx;
+		div -= 1.0;
 	}
-	return newfloatobject(fmod(v->ob_fval, wx));
+	return newfloatobject(mod);
 }
 
 static object *
 float_divmod(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
 	double vx, wx;
 	double div, mod;
 	object *t;
-	if (!is_floatobject(w)) {
-		err_badarg();
-		return NULL;
-	}
-	wx = ((floatobject *)w) -> ob_fval;
+	wx = w->ob_fval;
 	if (wx == 0.0) {
 		err_setstr(ZeroDivisionError, "float divmod()");
 		return NULL;
@@ -245,15 +229,11 @@
 static object *
 float_pow(v, w)
 	floatobject *v;
-	object *w;
+	floatobject *w;
 {
 	double iv, iw, ix;
-	if (!is_floatobject(w)) {
-		err_badarg();
-		return NULL;
-	}
 	iv = v->ob_fval;
-	iw = ((floatobject *)w)->ob_fval;
+	iw = w->ob_fval;
 	/* Sort out special cases here instead of relying on pow() */
 	if (iw == 0.0)
 		return newfloatobject(1.0); /* x**0 is 1, even 0**0 */
@@ -347,10 +327,3 @@
 	0,			/*tp_as_sequence*/
 	0,			/*tp_as_mapping*/
 };
-
-/*
-XXX This is not enough.  Need:
-- automatic casts for mixed arithmetic (3.1 * 4)
-- mixed comparisons (!)
-- look at other uses of ints that could be extended to floats
-*/