Opened 7 years ago

Closed 7 years ago

#4038 closed Bugs (fixed)

hash_value(float) collision

Reported by: dsharlet@… Owned by: Daniel James
Milestone: Boost 1.43.0 Component: hash
Version: Boost 1.41.0 Severity: Problem
Keywords: hash_value collision float Cc:


the hash_value function for floats has at least one collision that is rather serious:

std::size_t zero = boost::hash_value(0.0f); std::size_t half = boost::hash_value(0.5f);

both of these have the same hash value. Why not just use reinterpret_cast <> for this hash function?

Attachments (0)

Change History (3)

comment:1 Changed 7 years ago by Daniel James

Status: newassigned

Because in cross platform development there's no guarantee that two floats with different binary representations will be unequal, and there's no guarantee that the binary representation will be the same size as std::size_t. I do use a binary algorithm on a limited number of platforms, and could for others, but it has to be done carefully and there are more important things to do.

comment:2 Changed 7 years ago by Daniel James

(In [60805]) Remove optimization which causes 0.5 to hash to 0. Refs #4038.

I have an internal requirement the 0 hashes to 0, a better solution might be to remove that, put the optimization back and hash 0 to another value. Or alternatively, use the main combine function instead.

comment:3 Changed 7 years ago by Daniel James

Resolution: fixed
Status: assignedclosed

(In [60980]) Merge from trunk.

  • Add quick_erase for unordered. erase_return_void is now deprecated. Fixes #3966
  • Avoid collision between 0 and 0.5. Fixes #4038

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Daniel James.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.