Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 浮点值哈希码的计算算法?_Algorithm_Hashcode - Fatal编程技术网

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

Algorithm 浮点值哈希码的计算算法?,algorithm,hashcode,Algorithm,Hashcode,我正在寻找一种算法,可以计算浮点类型或浮点对象的哈希代码。我在web上搜索了很长一段时间,但我得到的是一个抽象层,比如Java的floatToRawBits,其中我不知道Java的Float哈希代码的内部实现。我真正想要的是一个实现,而不是一个接口 我知道这看起来很奇怪,但我现在确实需要这个。有人能帮忙吗?谢谢 我的javadocs说floatorawintbits: /** * Returns a representation of the specified floating-point

我正在寻找一种算法,可以计算浮点类型或浮点对象的哈希代码。我在web上搜索了很长一段时间,但我得到的是一个抽象层,比如Java的floatToRawBits,其中我不知道Java的Float哈希代码的内部实现。我真正想要的是一个实现,而不是一个接口


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

我的javadocs说
floatorawintbits

/**
 * 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.
 */

实现将取决于您使用的JVM/JDK。对于openJDK,源代码是。从给出的许可证来看,openJDK和Oracle似乎使用相同的代码

floatToRawIntBits
的方法是

/*
 * Find the bit pattern corresponding to a given float, NOT collapsing NaNs
 */
JNIEXPORT jint JNICALL
Java_java_lang_Float_floatToRawIntBits(JNIEnv *env, jclass unused, jfloat v)
{
    union {
        int i;
        float f;
    } u;
    u.f = (float)v;
    return (jint)u.i;
}

您还没有提供哈希代码的大小,我相信它在这里有点关键。源代码是。看起来实际调用的方法是本机代码。我也看到了这些,但一点帮助都没有。floatToRawIntBits上的docblocks不能说明它是如何实现的,理想情况下,我可以查看本机C代码以了解它是如何实现的。然后查看@Trengot answer,它看起来像您想要的。
/*
 * Find the bit pattern corresponding to a given float, NOT collapsing NaNs
 */
JNIEXPORT jint JNICALL
Java_java_lang_Float_floatToRawIntBits(JNIEnv *env, jclass unused, jfloat v)
{
    union {
        int i;
        float f;
    } u;
    u.f = (float)v;
    return (jint)u.i;
}