sizeof长双精度不匹配? [英] sizeof long double and precision not matching?

查看:198
本文介绍了sizeof长双精度不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的C代码:

  #include< stdio.h> 
int main(int argc,char * argv [])
{
const long double ld = 0.12345678901234567890123456789012345L;
printf(%lu%.36Lf\\\
,sizeof(ld),ld);
返回0;



$ b编译时使用 gcc 4.8.1 $ > Ubuntu x64 13.04 ,它打印:

  16 0.123456789012345678901321800735590983 

这告诉我一个长双重的16个字节,但小数似乎只有20地点。这怎么可能? 16字节对应一个四元组,四元组会给我33至36个十进制数。

解决方案 10 (2 <64> )数字,大约为20位数。

Consider the following C code:

#include <stdio.h>
int main(int argc, char* argv[]) 
{
    const long double ld = 0.12345678901234567890123456789012345L;
    printf("%lu %.36Lf\n", sizeof(ld), ld);
    return 0;
}

Compiled with gcc 4.8.1 under Ubuntu x64 13.04, it prints:

16 0.123456789012345678901321800735590983

Which tells me that a long double weights 16 bytes but the decimals seems to be ok only to the 20th place. How is it possible? 16 bytes corresponds to a quad, and a quad would give me between 33 and 36 decimals.

解决方案

The long double format in your C implementation uses an Intel format with a one-bit sign, a 15-bit exponent, and a 64-bit significand (ten bytes total). The compiler allocates 16 bytes for it, which is wasteful but useful for some things such as alignment. However, the 64 bits provide only log10(264) digits of significance, which is about 20 digits.

这篇关于sizeof长双精度不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆