计算浮点值哈希码的算法?

标签 algorithm hashcode

我一直在寻找一种算法来计算浮点类型或浮点对象的哈希码。我在网上搜索了一段时间,但我得到的是像 Java 的 floatToRawBits 这样的抽象层,我不知道 Java 的 float 哈希码的内部实现。我真正想要的是一个实现,而不是一个接口(interface)。

我知道这可能看起来很奇怪,但我现在确实需要它。有人可以帮忙吗?谢谢。

最佳答案

我的 javadocs 说 floatToRawIntBits:

/**
 * Returns a representation of the specified floating-point value
 * according to the IEEE 754 floating-point "single format" bit
 * layout, preserving Not-a-Number (NaN) values.
 *
 * <p>Bit 31 (the bit that is selected by the mask
 * {@code 0x80000000}) represents the sign of the floating-point
 * number.
 * Bits 30-23 (the bits that are selected by the mask
 * {@code 0x7f800000}) represent the exponent.
 * Bits 22-0 (the bits that are selected by the mask
 * {@code 0x007fffff}) represent the significand (sometimes called
 * the mantissa) of the floating-point number.
 */

看起来很简单。然后看Float.floatToIntBits(value)实现:

int result = floatToRawIntBits(value);

// Check for NaN based on values of bit fields, maximum
// exponent and nonzero significand.
if ( ((result & FloatConsts.EXP_BIT_MASK) ==
      FloatConsts.EXP_BIT_MASK) &&
     (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
    result = 0x7fc00000;
return result;

关于计算浮点值哈希码的算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990001/

相关文章:

javascript - 浏览器误读 Document.write()

algorithm - 无向图中的桥梁确定

java - 由于 "hashCode()",HashMap.get() 未返回正确的值

c# - 我如何通过哈希码找到一个对象?

Java - 整数变量中的 hashCode() 返回分配的值

c - C 中 float 的基数排序 - 负值被破坏

在允许的重量范围内配对 2 个对象的算法?

java - 不同的哈希码但 HashMap 不起作用

java - Netbeans 插入的 equals 和 hashCode 不显示

algorithm - 给定输入和时间的时间复杂度