Date: Sat, 28 Nov 2026 14:16:32 +0300
This is how it is currently implemented in stdc++:
162 /// Specialization for float.
163 template<>
164 <https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00762.html>
struct
hash <https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html><float>
: public __hash_base<size_t, float>
165 {
166 size_t
167 operator()(float __val) const noexcept
168 {
169 // 0 and -0 both hash to zero.
170 return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
171 }
172 };
And _Hash_impl just does a "bitwise-like" hashing - it uses all the bits
for hashing:
126 struct _Hash_impl
127 {
128 static size_t
129 hash
<https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html>(const
void* __ptr, size_t __clength,
130 size_t __seed = static_cast<size_t>(0xc70f6907UL))
131 { return _Hash_bytes(__ptr, __clength, __seed); }
132
133 template<typename _Tp>
134 static size_t
135 hash
<https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html>(const
_Tp& __val)
136 { return hash
<https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html>(&__val,
sizeof(__val)); }
It is not in accordance with what Mr. Knuth told us at the university :D,
since there is always an error - and in practice, we should always take it
into account: fabs(float1 - float2) <= error1 + error2.
пт, 27 нояб. 2026 г. в 21:48, Paul Caprioli via Std-Proposals <
std-proposals_at_[hidden]>:
> > 0.0 == -0.0 (On x86)
>
> And on any machine supporting IEEE Std 754-2019 floating-point
> arithmetic. "Comparisons shall ignore the sign of zero."
>
> > two NaNs will compare as not equal, but would (could?) memcmp as
> > equal.
>
> Yes, "Every NaN shall compare unordered with everything, including
> itself."
>
> Could. NaNs have a payload. For example, in single precision, both
> 0x7FC00000 and 0x7FC0DEAD are NaN.
>
>
> S ---E8--- ----------F23----------
> Binary: 0 11111111 10000000000000000000000
>
> S ---E8--- ----------F23----------
> Binary: 0 11111111 10000001101111010101101
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
162 /// Specialization for float.
163 template<>
164 <https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00762.html>
struct
hash <https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html><float>
: public __hash_base<size_t, float>
165 {
166 size_t
167 operator()(float __val) const noexcept
168 {
169 // 0 and -0 both hash to zero.
170 return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
171 }
172 };
And _Hash_impl just does a "bitwise-like" hashing - it uses all the bits
for hashing:
126 struct _Hash_impl
127 {
128 static size_t
129 hash
<https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html>(const
void* __ptr, size_t __clength,
130 size_t __seed = static_cast<size_t>(0xc70f6907UL))
131 { return _Hash_bytes(__ptr, __clength, __seed); }
132
133 template<typename _Tp>
134 static size_t
135 hash
<https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html>(const
_Tp& __val)
136 { return hash
<https://gcc.gnu.org/onlinedocs/gcc-4.8.0/libstdc++/api/a00743.html>(&__val,
sizeof(__val)); }
It is not in accordance with what Mr. Knuth told us at the university :D,
since there is always an error - and in practice, we should always take it
into account: fabs(float1 - float2) <= error1 + error2.
пт, 27 нояб. 2026 г. в 21:48, Paul Caprioli via Std-Proposals <
std-proposals_at_[hidden]>:
> > 0.0 == -0.0 (On x86)
>
> And on any machine supporting IEEE Std 754-2019 floating-point
> arithmetic. "Comparisons shall ignore the sign of zero."
>
> > two NaNs will compare as not equal, but would (could?) memcmp as
> > equal.
>
> Yes, "Every NaN shall compare unordered with everything, including
> itself."
>
> Could. NaNs have a payload. For example, in single precision, both
> 0x7FC00000 and 0x7FC0DEAD are NaN.
>
>
> S ---E8--- ----------F23----------
> Binary: 0 11111111 10000000000000000000000
>
> S ---E8--- ----------F23----------
> Binary: 0 11111111 10000001101111010101101
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2026-11-28 11:16:49
