Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtables/Dictionaries that use floats/doubles

I read somewhere about other data structures similar to hashtables, dictionaries but instead of using ints, they were using floats/doubles, etc.

Anyone knows what they are?

like image 348
Joan Venge Avatar asked Dec 20 '25 02:12

Joan Venge


2 Answers

If you mean using floats/doubles as keys in your hash, that's easy. For example, in .NET, it's just using Dictionary<double,MyValueType>.

If you're talking about having the hash be based off a double instead of an int....

Technically, you can have any element as your internal hash. Normally, this is done using an int or long, since these are fast, and the hashing algorithm is easy to compute.

However, the hash is really just a BitArray at heart, so anything would work. There really isn't much advantage to making this something other than an int or long, other than potentially allowing a larger set of hash values (ie: if you go to an 8 byte or larger type for your hash).

like image 153
Reed Copsey Avatar answered Dec 23 '25 02:12

Reed Copsey


You mean as keys? That strikes me as tricky.

If you're using them as arbitrary keys, they're no better than integers.

If you expect to calculate a floating-point value and use it to look something up in a hash table, you're living very dangerously. Floating point numbers do not have infinite precision, and calculating the same thing in two slightly different ways can result in very tiny differences in the result. Hash keys rely on getting the exact same thing every time, so you'd have to be careful to round, and round in exactly the same way at all times. This is trickier than it sounds, by the way.

So, what would you do with floating-point hashes?

like image 35
David Thornley Avatar answered Dec 23 '25 02:12

David Thornley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!