hash and equal problem

Boris Gaertner Boris.Gaertner at gmx.net
Tue Mar 28 06:46:03 UTC 2006


"nicolas cellier" <ncellier at ifrance.com>



> Hi all,
>
> i do not think that this behavior is correct
>   (2 = 2.0) = true.
>   (2 hash = 2.0 hash) = false.
>
> i saw a discussion about it in an old old thread (1999), but did not
> understand the rationale for such a behavior.
>
> i can exhibit a lot of examples of code completely broken by this
behavior.
>
> Try this one:
>
>   | aSet |
>   aSet := Set new.
>   aSet add: #( 1.0 2 3 4 5 6).
>   aSet add: #( 1 2.0 3 4 5 6).
>   aSet add: #( 1 2 3.0 4 5 6).
>   aSet add: #( 1 2 3 4.0 5 6).
>   aSet add: #( 1 2 3 4 5.0 6).
>   aSet add: #( 1 2 3 4 5 6.0).
>   aSet size
>
> Then, try this slight modification:
>
>   | aSet |
>   aSet := Set new: 12.
>   aSet add: #( 1.0 2 3 4 5 6).
>   aSet add: #( 1 2.0 3 4 5 6).
>   aSet add: #( 1 2 3.0 4 5 6).
>   aSet add: #( 1 2 3 4.0 5 6).
>   aSet add: #( 1 2 3 4 5.0 6).
>   aSet add: #( 1 2 3 4 5 6.0).
>   aSet size
>
> You will get 6 or 5 depending on initial Set capacity...

Wow, this is a very impressive example, thank you a lot.

VisualWorks, Dolphin and VisualAge for Smalltalk
use different algorithms to ensure that a float fl that satiyfies
the condition  fl = fl truncated has the same hash
as the integer  fl truncated.

In Visual age this is done with:

Float >> hash
 "Answer a unique Integer which represents a hash for the receiver."

 ^self truncated hash


the other systems use something like


float>>hash
   | tr |
  tr := self truncated.
  self = tr
    ifTrue: [^tr hash]
   ifFalse: [ " some computation " ]



Your example works also with fractions,




More information about the Squeak-dev mailing list