#19304: Fix hash function of rationals
-------------------------------------+-------------------------------------
       Reporter:  vdelecroix         |        Owner:
           Type:  enhancement        |       Status:  needs_info
       Priority:  critical           |    Milestone:  sage-6.9
      Component:  misc               |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Jeroen Demeyer     |    Reviewers:  Vincent Delecroix
Report Upstream:  N/A                |  Work issues:
         Branch:                     |       Commit:
  u/jdemeyer/fix_hash_function_of_rationals|  
c3d64c2bde1bf8a5c64c9d433e1d94004b8250ef
   Dependencies:                     |     Stopgaps:
-------------------------------------+-------------------------------------
Changes (by vdelecroix):

 * status:  needs_review => needs_info
 * reviewer:   => Vincent Delecroix


Comment:

 There are fractions in Python3, shouldn't we try to be complient with it?
 {{{
 >>> import fractions
 >>> fractions.Fraction(2,3)
 Fraction(2, 3)
 >>>  hash(fractions.Fraction(2,3))
 768614336404564651
 }}}
 The code is actually pure python and takes care of exactly representable
 float... and Python seems to have a different politics than Sage to that
 respect.
 {{{
     def __hash__(self):
         """hash(self)

         Tricky because values that are exactly representable as a
         float must have the same hash as that float.

         """
         # XXX since this method is expensive, consider caching the result
         if self._denominator == 1:
             # Get integers right.
             return hash(self._numerator)
         # Expensive check, but definitely correct.
         if self == float(self):
             return hash(float(self))
         else:
             # Use tuple's hash to avoid a high collision rate on
             # simple fractions.
             return hash((self._numerator, self._denominator))
 }}}

--
Ticket URL: <http://trac.sagemath.org/ticket/19304#comment:9>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to