diff Objects/floatobject.c @ 1257:1cabc2b6c970 legacy-trunk

* Changed all copyright messages to include 1993. * Stubs for faster implementation of local variables (not yet finished) * Added function name to code object. Print it for code and function objects. THIS MAKES THE .PYC FILE FORMAT INCOMPATIBLE (the version number has changed accordingly) * Print address of self for built-in methods * New internal functions getattro and setattro (getattr/setattr with string object arg) * Replaced "dictobject" with more powerful "mappingobject" * New per-type functio tp_hash to implement arbitrary object hashing, and hashobject() to interface to it * Added built-in functions hash(v) and hasattr(v, 'name') * classobject: made some functions static that accidentally weren't; added __hash__ special instance method to implement hash() * Added proper comparison for built-in methods and functions
author Guido van Rossum <guido@python.org>
date Mon, 29 Mar 1993 10:43:31 +0000
parents c4cd0d9d8281
children 2919b448e385
line wrap: on
line diff
--- a/Objects/floatobject.c	Sat Mar 27 18:11:32 1993 +0000
+++ b/Objects/floatobject.c	Mon Mar 29 10:43:31 1993 +0000
@@ -145,6 +145,38 @@
 	return (i < j) ? -1 : (i > j) ? 1 : 0;
 }
 
+static long
+float_hash(v)
+	floatobject *v;
+{
+	double intpart, fractpart;
+	int expo;
+	long x;
+	/* This is designed so that Python numbers with the same
+	   value hash to the same value, otherwise comparisons
+	   of mapping keys will turn out weird */
+	fractpart = modf(v->ob_fval, &intpart);
+	if (fractpart == 0.0) {
+		if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) {
+			/* Convert to long int and use its hash... */
+			object *w = dnewlongobject(v->ob_fval);
+			if (w == NULL)
+				return -1;
+			x = hashobject(w);
+			DECREF(w);
+			return x;
+		}
+		x = (long)intpart;
+	}
+	else {
+		fractpart = frexp(fractpart, &expo);
+		x = (long) (intpart + fractpart) ^ expo; /* Rather arbitrary */
+	}
+	if (x == -1)
+		x = -2;
+	return x;
+}
+
 static object *
 float_add(v, w)
 	floatobject *v;
@@ -378,4 +410,5 @@
 	&float_as_number,	/*tp_as_number*/
 	0,			/*tp_as_sequence*/
 	0,			/*tp_as_mapping*/
+	float_hash,		/*tp_hash */
 };