diff Python/bltinmodule.c @ 1215:9d6cf529bb71 legacy-trunk

bltinmodule.c: added round(x, [n]); coerce() of two class instances will try to coerce anyway. classobject.c: instance 'nonzero' should first try __nonzero__ only then __len__.
author Guido van Rossum <guido@python.org>
date Fri, 12 Feb 1993 16:29:05 +0000
parents ce718457b172
children c4cd0d9d8281
line wrap: on
line diff
--- a/Python/bltinmodule.c	Wed Feb 10 14:10:56 1993 +0000
+++ b/Python/bltinmodule.c	Fri Feb 12 16:29:05 1993 +0000
@@ -616,6 +616,34 @@
 }
 
 static object *
+builtin_round(self, args)
+	object *self;
+	object *args;
+{
+	extern double floor PROTO((double));
+	extern double ceil PROTO((double));
+	double x;
+	double f;
+	int ndigits = 0;
+	int sign = 1;
+	int i;
+	if (!getargs(args, "d", &x)) {
+		err_clear();
+		if (!getargs(args, "(di)", &x, &ndigits))
+			return NULL;
+	}
+	f = 1.0;
+	for (i = ndigits; --i >= 0; )
+		f = f*10.0;
+	for (i = ndigits; ++i <= 0; )
+		f = f*0.1;
+	if (x >= 0.0)
+		return newfloatobject(floor(x*f + 0.5) / f);
+	else
+		return newfloatobject(ceil(x*f - 0.5) / f);
+}
+
+static object *
 builtin_str(self, v)
 	object *self;
 	object *v;
@@ -674,6 +702,7 @@
 	{"raw_input",	builtin_raw_input},
 	{"reload",	builtin_reload},
 	{"repr",	builtin_repr},
+	{"round",	builtin_round},
 	{"setattr",	builtin_setattr},
 	{"str",		builtin_str},
 	{"type",	builtin_type},
@@ -766,7 +795,7 @@
 	register object *w = *pw;
 	int res;
 
-	if (v->ob_type == w->ob_type) {
+	if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
 		INCREF(v);
 		INCREF(w);
 		return 0;