[Python-Dev] Decimal <-> float comparisons in py3k. (original) (raw)
Nick Coghlan ncoghlan at gmail.com
Fri Mar 19 13:03:25 CET 2010
- Previous message: [Python-Dev] Decimal <-> float comparisons in py3k.
- Next message: [Python-Dev] Decimal <-> float comparisons in py3k.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Mark Dickinson wrote:
On Fri, Mar 19, 2010 at 9:37 AM, Mark Dickinson <dickinsm at gmail.com> wrote: For ints, this hash function is almost identical to what Python already has, except that the current int hash does a reduction modulo 232-1 or 264-1 rather than 2**31-1. For all small ints, hash(n) == n, as currently. Either way, the hash can be computed digit-by-digit in exactly the same manner. For floats, it's also easy to compute: express the float as m * 2**e for some integers m and e, compute hash(m), and rotate e bits in the appropriate direction. And it's straightforward to implement for the Decimal and Fraction types, too.
It seems to me that given the existing conflation of numeric equivalence and containment testing, going the whole hog and fixing the set membership problem for all of our rational types would be the right thing to do.
Would it be worth providing the underlying implementation of the hash algorithm as a math.hash_rational function and then use it for Decimal and Fraction?
That would have the virtue of making it easy for others to define numeric types that "played well" with numeric equivalence.
Cheers, Nick.
-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
- Previous message: [Python-Dev] Decimal <-> float comparisons in py3k.
- Next message: [Python-Dev] Decimal <-> float comparisons in py3k.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]