You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A discussion about the implementation of hash was started in #378. While that issue has a narrower scope (hashing of Quantity{BigFloat} where the BigFloats have the same values (==) but are not identical (===)), hashing should generally be consistent with isequal (see the hash docstring), i.e., hash(1u"m") == hash(100u"cm").
Implementing such a hash method would potentially be very breaking (for example, right now one can have a Dict with both 1u"m" and 100u"cm" as keys), so it is something to consider for a 2.0 release.
Some observations/thoughts related to such a hash implementation:
To implement such a hash function, a quantity would have to be converted to a “standard unit” (probably upreferred). However, floating-point quantities might break this, since isequal does not necessarily convert its arguments to upreferred (it uses promote, which only falls back to upreferred for FreeUnits). I can think of two possible solutions to this:
Change isequal to always convert to upreferred.
Use TwicePrecision (is it sufficient?).
The Dates stdlib already implements hashing like this: hash(Second(60)) == hash(Minute(1)). However, they don’t have to deal with floating-point numbers, which makes it easier. If comparison between Quantitys and Periods is added to Unitful (see Add support for Dates.FixedPeriod and Dates.CompoundPeriod #331), hashing of Quantitys should also be consistent with hashing of Periods.
A discussion about the implementation of
hashwas started in #378. While that issue has a narrower scope (hashing ofQuantity{BigFloat}where theBigFloats have the same values (==) but are not identical (===)), hashing should generally be consistent withisequal(see thehashdocstring), i.e.,hash(1u"m") == hash(100u"cm").Implementing such a
hashmethod would potentially be very breaking (for example, right now one can have aDictwith both1u"m"and100u"cm"as keys), so it is something to consider for a 2.0 release.Some observations/thoughts related to such a
hashimplementation:hashfunction, a quantity would have to be converted to a “standard unit” (probablyupreferred). However, floating-point quantities might break this, sinceisequaldoes not necessarily convert its arguments toupreferred(it usespromote, which only falls back toupreferredforFreeUnits). I can think of two possible solutions to this:isequalto always convert toupreferred.TwicePrecision(is it sufficient?).Datesstdlib already implements hashing like this:hash(Second(60)) == hash(Minute(1)). However, they don’t have to deal with floating-point numbers, which makes it easier. If comparison betweenQuantitys andPeriods is added to Unitful (see Add support forDates.FixedPeriodandDates.CompoundPeriod#331), hashing ofQuantitys should also be consistent with hashing ofPeriods.