Hash function for floats

前端 未结 6 1604
野趣味
野趣味 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:57

    If you're interested, I just made a hash function that uses floating point and can hash floats. It also passes SMHasher ( which is the main bias-test for non-crypto hash functions ). It's a lot slower than normal non-cryptographic hash functions due to the float calculations.

    I'm not sure if tifuhash will become useful for all applications, but it's interesting to see a simple floating point function pass both PractRand and SMHasher.

    The main state update function is very simple, and looks like:

    function q( state, val, numerator, denominator ) {
      // Continued Fraction mixed with Egyptian fraction "Continued Egyptian Fraction"
      // with denominator = val + pos / state[1]
      state[0] += numerator / denominator;
      state[0] = 1.0 / state[0];
    
      // Standard Continued Fraction with a_i = val, b_i = (a_i-1) + i + 1
      state[1] += val;
      state[1] = numerator / state[1];
    }
    

    Anyway, you can get it on npm Or you can check out the github

    Using is simple:

    const tifu = require('tifuhash');
    
    const message = 'The medium is the message.';
    const number = 333333333;
    const float = Math.PI;
    
    console.log( tifu.hash( message ), 
      tifu.hash( number ),
      tifu.hash( float ),
    tifu.hash( ) );
    

    There's a demo of some hashes on runkit here https://runkit.com/593a239c56ebfd0012d15fc9/593e4d7014d66100120ecdb9

    Side note: I think that in future using floating point,possibly big arrays of floating point calculations, could be a useful way to make more computationally-demanding hash functions in future. A weird side effect I discovered of using floating point is that the hashes are target dependent, and I surmise maybe they could be use to fingerprint the platforms they were calculated on.

提交回复
热议问题