C/C++中与Java的doubleToRawLongBits()等效的函数是什么?

6
在Java中,Double.doubleToLongBits()方法对于实现hashCode()方法非常有用。
我正在尝试在C++中做同样的事情,并编写自己的doubleToRawLongBits()方法,因为在通过Google搜索后,我找不到合适的实现方式。
我可以从std::frexp(numbr,&exp)中获取有效位和指数,并确定符号,但无法弄清楚如何使用位运算符来获得Java等效的结果。
例如,Java的Double.doubleToLongBits()对于双精度数3.94返回以下值:
4616054510065937285
谢谢任何帮助。
Graham
以下是从Double.doubleToRawLongBits()复制并粘贴的文档。
===Java Double.doubleToRawLongBits() description===

/**
     * Returns a representation of the specified floating-point value
     * according to the IEEE 754 floating-point "double
     * format" bit layout, preserving Not-a-Number (NaN) values.
     * <p>
     * Bit 63 (the bit that is selected by the mask 
     * <code>0x8000000000000000L</code>) represents the sign of the 
     * floating-point number. Bits 
     * 62-52 (the bits that are selected by the mask 
     * <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0 
     * (the bits that are selected by the mask 
     * <code>0x000fffffffffffffL</code>) represent the significand 
     * (sometimes called the mantissa) of the floating-point number. 
     * <p>
     * If the argument is positive infinity, the result is
     * <code>0x7ff0000000000000L</code>.
     * <p>
     * If the argument is negative infinity, the result is
     * <code>0xfff0000000000000L</code>.
     * <p>
     * If the argument is NaN, the result is the <code>long</code>
     * integer representing the actual NaN value.  Unlike the
     * <code>doubleToLongBits</code> method,
     * <code>doubleToRawLongBits</code> does not collapse all the bit
     * patterns encoding a NaN to a single &quot;canonical&quot; NaN
     * value.
     * <p>
     * In all cases, the result is a <code>long</code> integer that,
     * when given to the {@link #longBitsToDouble(long)} method, will
     * produce a floating-point value the same as the argument to
     * <code>doubleToRawLongBits</code>.
     *
     * @param   value   a <code>double</code> precision floating-point number.
     * @return the bits that represent the floating-point number.
     * @since 1.3
     */
    public static native long doubleToRawLongBits(double value);

C语言的主要问题是双精度二进制形式未定义。 - bestsss
3个回答

8
一次简单的转换就可以完成:
double d = 0.5;

const unsigned char * buf = reinterpret_cast<const unsigned char *>(&d);

for (unsigned int i = 0; i != sizeof(double); ++i)
  std::printf("The byte at position %u is 0x%02X.\n", i, buf[i]);

符号位和指数位的位置取决于您的平台和字节序。如果您的浮点数是IEEE754格式,而且符号和指数位在前面,且CHAR_BIT == 8,您可以尝试以下方法:

const bool sign = buf[0] & 0x80;
const int exponent = ((buf[0] & 0x7F) << 4) + (buf[1] >> 4) - 1023;

在C语言中,如果要进行强制类型转换,可以使用(const unsigned char *)(&d)

更新:如果想要创建一个与原始数据位相同的整数,需要先创建整数,然后再进行拷贝操作。

unsigned long long int u;
unsigned char * pu = reinterpret_cast<unsigned char *>(&u);
std::copy(buf, buf + sizeof(double), pu);

在进行此操作时,您需要记住几件事情:整数的大小必须足够(使用静态断言sizeof(double) <= sizeof(unsigned long long int)),如果整数实际上更大,则只需将其复制到其中的部分。虽然我相信您会想出解决方法 :-)(如果您确实需要,可以使用一些模板魔法来创建正确大小的整数。)


如何从常数unsigned char * buf生成一个长整型? - Graham Seed

5
#include <stdint.h>

static inline uint64_t doubleToRawBits(double x) {
    uint64_t bits;
    memcpy(&bits, &x, sizeof bits);
    return bits;
}

这假设一个双精度浮点数的大小为64位,但并非总是如此。 - Rob K
1
@RobK:当然。但在double不映射到IEEE-754双精度浮点数的平台上,位表示对于期望该类型的程序来说可能并不是非常有用的。 - Stephen Canon
从原问题中提到的hashcode()函数来看,他似乎只是想要表示双精度浮点数的唯一比特串,以便在哈希函数或类似情况下使用。 - Rob K
是的,例如,可以使用Java编写Point3D对象的hashCode(); 实际上可以使用NetBeans轻松自动生成:public int hashCode() { int hash = 3; hash = 59 *哈希值+(int)(Double.doubleToLongBits(this.mx)^(Double.doubleToLongBits(this.mx)>>> 32)); hash = 59 *哈希值+(int)(Double.doubleToLongBits(this.my)^(Double.doubleToLongBits(this.my)>>> 32)); hash = 59 *哈希值+(int)(Double.doubleToLongBits(this.mz)^(Double.doubleToLongBits(this.mz)>>> 32)); 返回哈希值; } - Graham Seed
1
上面的doubleToRawBits()使用memcpy()对于3.94的示例与Java完全相同。这也是最简单的解决方案。 - Graham Seed

3

对于这些事情,我喜欢使用联合体。

union double_and_buffer {
    double d;
    unsigned char byte_buff[ sizeof(double) ];
} dab;

dab.d = 1.0;
for ( int i = 0; i < sizeof(dab.byte_buff); ++i )
{
    cout << hex byte_buff[ i ];
}

我认为这可以更清晰地表明你正在做什么,并让编译器完成所有计算。

我经常使用这种方法,但请注意,许多当前的编译器会在未明确禁用基于类型的别名分析时破坏它。 - Stephen Canon
1
@StephenCanon 我知道理论上读取未设置的联合成员是未定义行为,但真的有编译器会对这种众所周知且广泛使用的习惯造成问题吗? - Voo
1
@Voo:这些年来已经有过几个版本,最值得一提的是大约在4.1左右的一系列GCC构建。 - Stephen Canon
这是未定义的行为。此页面上的其他解决方案是正确的。 - Arnaud
微软在其API中广泛使用这种结构,例如在USB_HUB_CAP_FLAGS中。标准要求“联合体的大小足以容纳其非静态数据成员中最大的一个。每个非静态数据成员都被分配为一个结构体的唯一成员。联合体对象的所有非静态数据成员具有相同的地址”(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf,第9.5节)。因此,在这种方式下使用联合体等效于对所需变量的地址进行reinterpret_cast<>()。 - Rob K
说真的,你还有什么理由再使用联合体呢?它还有什么用处呢? - Rob K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接