Is it safe to use float as hash table keys?

I need to keep pairs float,intin which the value intstores the number of occurrences of the value floatinside the model that I use for the tool that I am developing, and I was wondering if it is safe to do such things.

Ultimate accuracy should be a problem when talking floats are used for direct comparison (or as content that needs to be hashed), so I think this approach is not recommended, am I right?

Actually, the problem is that I have no other information in combination with these floats, so I just can not use anything else as a key for the hash table, but at the same time, since there will be many keys, having good performance will be enjoyable.

Perhaps the best solution is to use a binary search tree (or an even more advanced data structure) to get at least the average case of O (logn), also if the constant coefficient would be better.

Do you have any suggestions? Just to let you know that I am developing in OCaml, but I think that these considerations can be considered language agnostic

+5
source share
4 answers

, . , , , . ( , ).

, - , . , , , . , , , , -.

+7

, .

-?

. , floats , - ( - )

- ?

, . , , , , , , . . , , .

float , int?

, OCaml a float (8 ). , , , # long, -. ( 8 ). , long ( , ).

, , float .

-, ?

, . , -. - . 2 ( ). , , swvitching .

+4

, , , .

, -, , , - , - . , sin(pi / 6) == 0.5, (2.0 / 3) * (2.0 / 3) == (4.0 / 9). LHS RHS.

, , , 0.5, sin(pi / 6), , , , .

, , . , 0,001, 0.2020001 0.2020003 " ", 0.1014999 0.1015001. base-10 , , , "float" .

. Hashtables , "", , - h, , x y "", h(x) == h(y). h "" ( h(x) == h(y) x != y), . . , , , , , .

, , , . , .

+1

?

: , . , , , " " .

0

All Articles