浮点的下界
C 中的浮点类型有下限吗?就像整数类型有下限(int 至少 16 位)?
Are there any lower bounds for floating point types in C? Like there are lower bounds for integral types (int being at least 16 bits)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。
float.h
包含常量,例如:FLT_EPSILON、DBL_EPSILON、LDBL_EPSILON
这是可用float
表示的最小非零值,double
和long double
表示形式。FLT_MAX
和FLT_MIN
表示float
可以表示的极端正数和负数。类似的DBL_
和LDBL_
也可用。FLT_DIG、DBL_DIG、LDBL_DIG
定义为十进制位数精度。您要求提供
xxx_MIN
或xxx_EPSILON
值。沿着这些思路,这里有一个问题,其中 我发布了一些显示 64 位 IEEE-754 浮点数内部结构的代码。
Yes.
float.h
contains constants such as:FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON
this is the least magnitude non-zero value which can be represented byfloat
,double
, andlong double
representations.FLT_MAX
andFLT_MIN
represent the extreme positive and negative numbers which can be represented forfloat
. SimilarDBL_
andLDBL_
are available.FLT_DIG, DBL_DIG, LDBL_DIG
are defined as the number of decimal digits precision.You are asking for either the
xxx_MIN
or thexxx_EPSILON
value.Along these lines, here is a question wherein I posted some code which displays the internals of a 64-bit IEEE-754 floating-point number.
float.h
包含许多描述浮点类型各种属性的宏(包括FLT_MIN
和DBL_MIN
)。标准中给出了
float.h
中限制要求的描述(C90 或 C99 - 5.2.4.2.2“浮点类型的特性”)。特别是,根据标准,任何实现都必须支持
float
或double
的下限至少为1E-37
。但实现可以做得比这更好(并在FLT_MIN
和DBL_MIN
中指出它的作用)。如果需要,请参阅此问题,了解如何获取标准文档副本的信息:
float.h
contains many macros describing various properties of the floating types (includingFLT_MIN
andDBL_MIN
).The description of the requirements of the limits in
float.h
is given in the standard (C90 or C99 - 5.2.4.2.2 "Characteristics of floating types").In particular, according to the standard any implementation must support a lower-bound of at least
1E-37
forfloat
ordouble
. But an implementation is free to do better than that (and indicate what it does inFLT_MIN
andDBL_MIN
).See this question for information on where to get a copy of the standards documents if you need one:
这里有用的参考是每个计算机科学家应该了解浮点知识算术。
浮点数的本质——它的大小、精度、限制——实际上是由硬件定义的,而不是由编程语言定义的。 x86 上的单精度浮点与 C、C#、Java 和任何其他实用编程语言中的单精度浮点相同。 (例外是在软件中实现奇数宽度浮点数的深奥编程语言。)
A useful reference here is What Every Computer Scientist Should Know About Floating-Point Arithmetic.
The nature of a floating point number — its size, precision, limits — is really defined by the hardware, rather than the programming language. A single-precision float on an x86 is the same in C, C#, Java, and any other practical programming language. (The exception is esoteric programming languages that implement odd widths of floating point number in software.)
标准草案 (n1401.pdf) 摘录
Wikipedia 有 一篇关于 IEC 559(或更确切地说 IEEE 754)的文章-1985)你可能会觉得有趣。
Excerpts from the Standard draft (n1401.pdf)
Wikipedia has an article about IEC 559 (or rather IEEE 754-1985) you might find interesting.