gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:22:06 28 4
gpt4 key购买 nike

我一直在寻找一种算法来计算浮点类型或浮点对象的哈希码。我在网上搜索了一段时间,但我得到的是像 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com