简体   繁体   English

无法将uint64_t正确转换为double。 我想念什么?

[英]Can't convert a uint64_t to a double properly. What am I missing?

To give some background, I'm coding the JVM for Java 8 in C, and I'm trying to print the Double value located in the Constant Pool. 为了提供一些背景信息,我正在用C对Java 8的JVM进行编码,并且试图打印位于常量池中的Double值。

I have two variables uint32_t that represent the high and low value of a double. 我有两个变量uint32_t代表双精度值的高低值。 I'm trying to print this double but I can't figure out what is wrong with my code. 我正在尝试将其打印出来,但是我无法弄清楚我的代码出了什么问题。

I've tried printing all values to check. 我尝试打印所有值进行检查。

uint64_t high_arg, low_arg, double_value;

high_arg = cp[cpIndex-1].info.Double.high_bytes; // value is = 0x0000000040000000
low_arg = cp[cpIndex-1].info.Double.low_bytes; // value is = 0x0000000000000000
double_value = (high_arg << 32) | low_arg; // value is = 0x4000000000000000
printf("%f\n", (double)double_value);

It prints double:4.61169e+18 but is was supposed to return 2 它打印出double:4.61169e+18但应该返回2

What am I missing? 我想念什么?

@JonathanLeffler明智地指出,解决方案是将表示double的整数转换为double指针,并打印其内容。

printf("%f", *(double *)&double_value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM