如何在C中确定FLT_DIG,DBL_DIG和LDBL_DIG [英] How are FLT_DIG, DBL_DIG, and LDBL_DIG determined in C

查看:234
本文介绍了如何在C中确定FLT_DIG,DBL_DIG和LDBL_DIG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FLT_DIG,DBL_DIG和LDBL_DIG是可以分别用浮点,双精度和长双精度类型精确表示的小数位数.

  #include< stdio.h>#include< float.h>int main(无效){printf(%d,%d,%d \ n",FLT_DIG,DBL_DIG,LDBL_DIG);返回0;} 

打印 6 15 18 .该标准在第5.2.4.2.2节中给出了准确的公式-例如对于float,p = 24,b = 2:

但是我不清楚上面的公式(否则")是如何得出的.有人可以解释吗?

以下是我遵循的未能回答问题的原因.考虑浮点类型,其有效位数为23位(IEEE-754标准).可以准确表示的最大二进制整数为:

  100 ... 00(总共25位数字,因为我们有一个隐式1)= 2 ^ 24= 10 ^(24 * log(2)) 

因此#个十进制数字:

  = floor(24 * log(2))= 7 

不是标准状态的 floor(23 * log(2))= 6 .

解决方案

如何确定FLT_DIG,DBL_DIG和LDBL_DIG(?)

大约,使用 p 个二进制数字,可以对p *(log 10 2)个十进制数字进行编码.每个二进制数字约占0.3十进制数字.

将浮点数和浮点数作为具有 n 个数字的十进制文本和一个指数以线性分布在较大的对数分布中./p>

下面的-1来自最坏情况的对齐问题,其中十进制值的分布相对于二进制值最密集.如果 float 的底数为10(因为分布对齐),则不为-1.

以下是我遵循的未能回答问题的原因....

此处中,#6的OP推理行失败.十进制值与二进制值的对齐并不总是有效".

一个 float ,例如 binary32 示例.

  1. 在[2 33 或8,589,934,592 ... 2 34 或17,179,869,184 ...)值经过 linear 编码:彼此相距1024.0.在子范围[9,000,000,000和10,000,000,000]中,大约有976,562个不同的值.

  2. 作为文本,范围为[9,000,000 * 10 3 和10,000,000 * 10 3 ),使用1个前导数字和6个尾随数字,有1,000,000个不同价值观.在同一范围内,每#1的 float 值都少于1,000,000.因此,一些十进制文本值将转换为相同的 float .在此范围内,我们可以使用6位(而不是7位)进行独特的转化.

FLT_DIG, DBL_DIG, LDBL_DIG are the number of decimal digits that can be accurately represented by float, double, and long double types respectively.

#include <stdio.h>
#include <float.h>

int main(void)
{
  printf("%d, %d, %d\n", FLT_DIG, DBL_DIG, LDBL_DIG);
  return 0;
}

prints 6, 15, and 18. The standard, at section 5.2.4.2.2, gives the accurate formula -- e.g. for float, p = 24, and b = 2:

But I'm not clear how the above formula (the "otherwise") is derived. Can someone please explain?

The below is the reasoning I followed which does not answer the question. Consider type float which has 23 bits in the significand (IEEE-754 standard). The maximum binary integer that can be accurately represented is:

  100...00 (25 digits total, because we have an implicit 1)
  = 2^24
  = 10^(24*log(2)) 

Hence # decimal digits:

= floor(24*log(2)) = 7

And not floor(23 * log(2)) = 6 as the standard states.

解决方案

How are FLT_DIG, DBL_DIG, and LDBL_DIG determined (?)

Roughly, with p binary digits, p*(log102) decimal digits can be encoded. Every binary digit contributes to about .3 decimal digits.

Recall floating point values and numbers as decimal text with n signification digits and an exponent are distributed linearly within a larger logarithmic distribution.

The -1 of below comes from worst case alignments issues where the distribution of decimal values is densest relative to the binary values. There is no -1 if the float base was 10, as the distribution aligns.

The below is the reasoning I followed which does not answer the question. ...

OP's line of reasoning fails per #6 in here. The alignment of decimal values to binary ones does not always "work".

A float as binary32 example.

  1. In the range [233 or 8,589,934,592 ... 234 or 17,179,869,184), again, 223 (8,388,608) values are linearly encoded: 1024.0 apart from each other. In the sub range [9,000,000,000 and 10,000,000,000), there are about 976,562 different values.

  2. As text, the range [9,000,000*103 and 10,000,000*103), using 1 lead digit and 6 trailings ones, there are 1,000,000 different values. Per #1, In the same range, there are less than 1,000,000 different float values. Thus some decimal textual values will convert to the same float. In this range we can use 6, not 7, digits for distinctive conversions.

这篇关于如何在C中确定FLT_DIG,DBL_DIG和LDBL_DIG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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