Hash function for floats

前端 未结 6 1609
野趣味
野趣味 2020-12-03 07:38

I\'m currently implementing a hash table in C++ and I\'m trying to make a hash function for floats...

I was going to treat floats as integers by padding the decimal

6条回答
  •  余生分开走
    2020-12-03 07:41

    If your hash function did the following you'd get some degree of fuzziness on the hash lookup

    unsigned int Hash( float f )
    {
        unsigned int ui;
        memcpy( &ui, &f, sizeof( float ) );
        return ui & 0xfffff000;
    }
    

    This way you'll mask off the 12 least significant bits allowing for a degree of uncertainty ... It really depends on yout application however.

提交回复
热议问题