summaryrefslogtreecommitdiffstats
path: root/Objects/floatobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-03-29 10:43:31 (GMT)
committerGuido van Rossum <guido@python.org>1993-03-29 10:43:31 (GMT)
commit9bfef44d97d1ae24e03717e3d59024b44626a9de (patch)
tree736b9f406d27f9dc4496f103cd395fb7a97401d4 /Objects/floatobject.c
parent4b1302bd1d211881178618aa8f41fa4460180f2e (diff)
downloadcpython-9bfef44d97d1ae24e03717e3d59024b44626a9de.zip
cpython-9bfef44d97d1ae24e03717e3d59024b44626a9de.tar.gz
cpython-9bfef44d97d1ae24e03717e3d59024b44626a9de.tar.bz2
* 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
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r--Objects/floatobject.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index e563583..8533e0e 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -145,6 +145,38 @@ float_compare(v, w)
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 @@ typeobject Floattype = {
&float_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
+ float_hash, /*tp_hash */
};