C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Q: invariant for unordered_set and unordered_map "hash" and "comp" argument

From: Aleksandr Povaliaev <apovalyaev_at_[hidden]>
Date: Sat, 28 Nov 2026 13:43:04 +0300
Yep. It is non-transitive. But this is how float and double comparison
works :)

Inventing "Hash" function for such case is not trivial, but still possible
- we should do a "rounding up" in a float-specific way, I think for any
"fl" being hashed we should skip all the "decimal digits" which might hold
an error and use them for hashing. Something like: find a minimal "ceil_fl"
where fabs(ceil_fl) > fabs(fl.m_value - m_error) and fabs(ceil_fl) >
fabs(fl.m_value + m_error), then skip all the "decimal digits" which are
different for fl.m_value - m_error and fl.m_value + m_error.

Truly speaking, I don't think that unordered_set is the best STL container
to mess with floats and doubles - I guess a "tree of segments"(from boost)
or something like that is more appropriate.

сб, 28 нояб. 2026 г. в 11:02, Lénárd Szolnoki <cpp_at_[hidden]>:

>
>
> On 28 November 2026 06:02:58 GMT, Aleksandr Povaliaev via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
> >A little correction to the previous post:
> >class CFloat {
> >private:
> > float m_value, m_error;
> >public:
> > operator == (const CFloat & right) {
> ><< return (right.m_value + m_value) <= right.m_error +
> >m_error;
> >>> return fabs(right.m_value - m_value) <= right.m_error +
> >m_error;
> > }
> > ....
> >}
>
> This CFloat type has a non-transitive operator==, looks like a horrible
> idea for hash keys.
>
> >
> >сб, 28 нояб. 2026 г. в 01:10, Александр Поваляев <apovalyaev_at_[hidden]>:
> >
> >> When dealing with float or double, you will need to use a class like
> >>
> >> class CFloat {
> >> private:
> >> float m_value, m_error;
> >> public:
> >> operator == (const CFloat & right) {
> >> return (right.m_value + m_value) <= right.m_error +
> m_error;
> >> }
> >> ....
> >> }
> >>
> >> or something like that. But again this logic will be hidden by
> >> encapsulation - it won't be "exported" to external "CompPred" function.
> >>
> >>
> >> пт, 27 нояб. 2026 г. в 23:34, Jonathan Wakely via Std-Proposals <
> >> std-proposals_at_[hidden]>:
> >>
> >>>
> >>>
> >>> On Fri, 27 Nov 2026, 18:04 Andre Kostur via Std-Proposals, <
> >>> std-proposals_at_[hidden]> wrote:
> >>>
> >>>> 0.0 == -0.0 (On x86)
> >>>>
> >>>> memcmp of doubles containing 0.0 and -0.0 does not return 0. And if
> >>>> I recall correctly, two NaNs will compare as not equal, but would
> >>>> (could?) memcmp as equal.
> >>>>
> >>>
> >>> There are many bit patterns that represent different NaNs. The same NaN
> >>> will compare equal using memcmp, different NaNs will compare not equal
> >>> using memcmp.
> >>>
> >>>
> >>>> On Fri, Nov 27, 2026 at 10:57 AM Александр Поваляев via Std-Proposals
> >>>> <std-proposals_at_[hidden]> wrote:
> >>>> >
> >>>> > Hi Sebastian,
> >>>> >
> >>>> > Why a bitwise comparison is inapplicable for a case when float or
> >>>> double is being used as a "Key" unordered_set template argument?
> >>>> >
> >>>> > Let's say, we have got a hash<float> function which uses all the
> bits
> >>>> of the float value and CompPred which does the same. It will work
> without
> >>>> any error. My attitude is the same: you either use bitwise comparison
> and
> >>>> bitwise hash function, or you use a class comparison operator
> (operator
> >>>> ==). In case of erroneous padding bits or some pointers, a class
> comparison
> >>>> operator is needed.
> >>>> >
> >>>> >
> >>>> > пт, 27 нояб. 2026 г. в 12:18, Sebastian Wittmeier via Std-Proposals
> <
> >>>> std-proposals_at_[hidden]>:
> >>>> >>
> >>>> >> And even that salient part would have to be normalized (for a
> planned
> >>>> bitwise compare) for some data formats, which allow non-unique
> >>>> representations of the same value. And even then it may be
> >>>> application-dependent. E.g. is a positive 0 equal to a negative 0?
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >> So even float or double as types perhaps used for the
> representation
> >>>> of the salient part, the bitwise comparison may be unapplicable.
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >> -----Ursprüngliche Nachricht-----
> >>>> >> Von: Jonathan Wakely via Std-Proposals <
> >>>> std-proposals_at_[hidden]>
> >>>> >> (An alternative design would be to have a projection function which
> >>>> just gives you the salient parts of the object, so you would call
> proj(obj)
> >>>> which would return const A&, and you could hash that and compare that
> for
> >>>> equality. But we don't have that design either.)
> >>>> >>
> >>>> >> --
> >>>> >> Std-Proposals mailing list
> >>>> >> Std-Proposals_at_[hidden]
> >>>> >> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>>> >
> >>>> > --
> >>>> > Std-Proposals mailing list
> >>>> > Std-Proposals_at_[hidden]
> >>>> > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>>> --
> >>>> Std-Proposals mailing list
> >>>> Std-Proposals_at_[hidden]
> >>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>>>
> >>> --
> >>> Std-Proposals mailing list
> >>> Std-Proposals_at_[hidden]
> >>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>>
> >>
>

Received on 2026-11-28 10:43:20