如何识别long double数据类型的精度

How to identify the precision of long double datatype

我的编译器告诉我 sizeof(long double) 的大小是 16 字节,这意味着它可以表示 2^128 以上的数字。现在,我想知道精度可以处理多少位数。例如,如果 x= 0.1234567812345678long double 可以在这里确定 x 的精确精度吗?

谢谢

header float.h 包含一些描述不同浮点数据类型精度的宏;例如LDBL_MANT_DIG 给出 long double.

的尾数中的二进制位数

如果你不懂浮点数的格式,推荐你阅读What Every Computer Scientist Should Know About Floating-Point Arithmetic

I want to know until how many digits the precision can handle.

LDBL_DIG,在<float.h>中,是将文本转换为long double distinctively[=41=的最小有效小数位数].它至少是 10,在您的平台上可能是 18 左右。 0.1234567812345678123.4567812345678 有 16 位有效小数位。


My compiler is telling me that the size of sizeof(long double) is 16 byte, which means it can represent a number up 2^128

如果 long double 是一个整数,但浮点数是分布的 . Further, some systems do not use all 16 bytes for data. Some are padding. ,这可能意味着类似的意思。使用 <float.h> 中的值来表征您的平台正在使用的 long double


if x= 0.1234567812345678, can long double identify the exact precision of x?

否,除非您使用的是使用十进制浮点数的罕见平台。

大多数平台使用二进制浮点数。因此,除非该数字可以表示为 2 的幂的精确总和,例如 42.125,否则保存为 long double 的值将是一个近似值。代码会将文本 0.1234567812345678 转换为 最近的 long double,它可能具有 0.1234567812345677972896140772718354128301143646240234375.

的精确值