A
A
Artyom_Kopan2021-11-15 21:26:59
Algorithms
Artyom_Kopan, 2021-11-15 21:26:59

How to hash real numbers?

A simple cast to int is either not suitable, because there is an infernal number of collisions, especially when close double values ​​\u200b\u200bare received as input. Which hash function is more efficient? memcpy(&doubleValue, &intValue, sizeof(double))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-11-15
@Artyom_Kopan

The problem with your code is that you only take the first 4 bytes out of 8 in double.
Copy to uint64_t. If you need fewer bits, then you can multiply by a large prime number and then take the xor of the least significant and most significant bits.

Армянское Радио, 2021-11-15
@gbg

Вещественные числа сами себе прекрасный 64-битный хэш.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question