繁体   English   中英

int64_t转换为“ long double”问题

[英]int64_t conversion to 'long double' issue

以下代码产生显示的输出,我感到困惑...我正在使用英特尔编译器版本2013 beta更新2 /opt/intel/composer_xe_2013.0.030/bin/intel64/icpc

// all good
int64_t fops_count1 = 719508467815;
long double fops_count2 = boost::static_cast<long double>(fops_count1);
printf("%" PRIu64 "\n", fops_count1); // OK outputs 719508467815
printf("%Le\n", fops_count2);         // OK outputs 7.195085e+11

// bad! why this?
int64_t fops_count1 = 18446743496931269238;
long double fops_count2 = boost::static_cast<long double>(fops_count1);
printf("%" PRIu64 "\n", fops_count1); // OK outputs 18446743496931269238
printf("%Le\n", fops_count2);         // FAIL outputs -5.767783e+11 <<<<<<<<<<<<<<<<< WHY?

忽略boost::static_cast ,我不明白,一个64位带符号整数不能代表您显示的数字,但是

18446743496931269238-2 64 = -576778282378

即,这是当二进制补码的64位有符号整数环绕时获得的值。

现在那是什么boost::static_cast

int64_t fops_count1 = 18446743496931269238;

这是带符号的溢出,即UB。 int64_t的最大值约为2 ^ 63,绝对小于此值。 好像您的处理器实现了环绕操作,给您看到的负值。

暂无
暂无

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

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