简体   繁体   English

c语言中double和long double数据类型有什么区别

[英]What is the difference between double and long double data type in c language

I am new in programming.我是编程新手。 When I run this code当我运行这段代码

#include <stdio.h>


int main()
{
    double  i = 6.4;
    double  *ptr = &i;
    printf("The valu of ptr is %u\n",ptr);
    ptr++;
    printf("The valu of ptr is %u\n",ptr);
    ptr++;
    printf("The valu of ptr is %u\n",ptr);
    

    return 0;
}

I get this output:我得到这个 output:

The valu of ptr is 3904284976
The valu of ptr is 3904284984
The valu of ptr is 3904284992

from this it is clear that double type data needs 8 byte in my memory.由此可以清楚地看出,我的 memory 中的 double 类型数据需要 8 个字节。

But when I run this code但是当我运行这段代码时

#include <stdio.h>


int main()
{
    long double  i = 6.4;
    long double  *ptr = &i;
    printf("The valu of ptr is %u\n",ptr);
    ptr++;
    printf("The valu of ptr is %u\n",ptr);
    ptr++;
    printf("The valu of ptr is %u\n",ptr);
    

    return 0;
}

I get this output我得到这个 output

The valu of ptr is 3519104768
The valu of ptr is 3519104784
The valu of ptr is 3519104800

From this it is seen that long double datatype needs 16 byte in memory.由此可见,memory 中的 long double 数据类型需要 16 个字节。

What is the major difference between double and long double data type in c? c 中的 double 和 long double 数据类型之间的主要区别是什么?

What is the major difference between double and long double data type in c? c 中的 double 和 long double 数据类型之间的主要区别是什么?

Quoting from N1570 section "6.2.5 Types" this is what the C standard says:引用 N1570 部分“6.2.5 类型”,这是 C 标准所说的:

There are three real floating types, designated as float, double, and long double.The set of values of the type float is a subset of the set of values of the type double;共有三种实浮点类型,分别指定为 float、double 和 long double。float 类型的值集是 double 类型值集的子集; the set of values of the type double is a subset of the set of values of the type long double double 类型的值集是 long double 类型的值集的子集

This means that long double is a floating point type having at least the same precision as the floating point type double .这意味着long double是一种浮点类型,其精度至少与浮点类型double相同。

So maybe long double is the same as double .所以也许long doubledouble相同。 Maybe it has better precision than double .也许它比double具有更好的精度。 The C standard only tells us that long double can't have worse precision than double . C 标准只告诉我们long double的精度不能比double差。

This is further expanded in Annex E of the standard where the "minimum requirements" for double and long double is the same.这在标准的附录 E 中进一步扩展,其中 double 和 long double 的“最低要求”相同。

So the difference - if any - depends on your system.所以差异 - 如果有的话 - 取决于您的系统。

Also notice that <float.h> on your system gives you information about the three floating point types on your system.另请注意,系统上的 <float.h> 为您提供有关系统上三种浮点类型的信息。 For that please refer to the "5.2.4.2.2 Characteristics of floating types <float.h>" of the standard - it's too much info to be pasted into this answer.为此,请参阅标准的“5.2.4.2.2 浮动类型 <float.h> 的特征” - 粘贴到此答案中的信息太多。

BTW:顺便提一句:

Instead of using pointer values you could have printed the size, like:您可以打印大小而不是使用指针值,例如:

printf("size of double %zu\n", sizeof(double));
printf("size of long double %zu\n", sizeof(long double));

If the sizes are different then it is very likely that long double has better precision than double .如果大小不同,那么很可能long double double更好的精度。

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

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