Skip to content

Hashing of Quantitys #379

@sostock

Description

@sostock

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions