@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
24 lines (18 loc) • 766 B
JavaScript
import { UINT32_MAX } from "../../binary/UINT32_MAX.js";
/**
*
* @param {number} v
* @returns {number}
*/
export function computeHashFloat(v) {
// we break the number up into fractional and whole parts
const whole = v >> 0;
const fraction = v - whole;
// fractional part is scaled up into uint32 range,
// this inexact method, as it will produce 0 hash for values below a certain threshold, but it's fast
const fraction_hash = ((fraction) * UINT32_MAX) >>> 0;
// we flip the order, tiny fractions will result in higher bits of the final hash, leads to better dispersion
const fraction_hash_inverse = UINT32_MAX - fraction_hash;
// take XOR of both parts
return fraction_hash_inverse ^ whole;
}