在Java Double.doubleToLongBits()
中,实现hashCode()
方法很有用.
我试图在C++中做同样的事情并编写我自己的doubleToRawLongBits()
方法,因为在浏览Google之后我找不到合适的实现.
我可以从中获取signif和exponent std::frexp(numbr,&exp)
并且可以确定符号但是无法弄清楚使用按位运算符来获得Java等价物.
例如,Java Double.doubleToLongBits()
为双3.94返回以下内容:
4616054510065937285
谢谢你的帮助.
格雷厄姆
以下是从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 "canonical" 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);
Run Code Online (Sandbox Code Playgroud)
一个简单的演员会做:
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]);
Run Code Online (Sandbox Code Playgroud)
符号位和指数位取决于您的平台和字节顺序.如果您的花车是IEE754,如果符号和指数在前面,如果CHAR_BIT == 8
,你可以试试这个:
const bool sign = buf[0] & 0x80;
const int exponent = ((buf[0] & 0x7F) << 4) + (buf[1] >> 4) - 1023;
Run Code Online (Sandbox Code Playgroud)
(在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);
Run Code Online (Sandbox Code Playgroud)
为此,你必须记住几件事:整数的大小必须足够(静态断言sizeof(double) <= sizeof(unsigned long long int)
应该做的伎俩),如果整数实际上更大,那么你只是复制到它的一部分.我相信你会明白这一点,但是:-)(你可以使用一些模板魔术来创建一个正确大小的整数,如果你真的想要的话.)
#include <stdint.h>
static inline uint64_t doubleToRawBits(double x) {
uint64_t bits;
memcpy(&bits, &x, sizeof bits);
return bits;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2284 次 |
最近记录: |