如何在C中打印尽可能大的float和double? [英] How would I print the largest possible float and double in C?
问题描述
对于以下代码,
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void) {
printf("double max = %??\n", DBL_MAX);
printf("double min = %??\n", DBL_MIN);
printf("double epsilon = %??\n", DBL_EPSILON);
printf("float epsilon = %??\n", FLT_EPSILON);
printf("float max = %??\n", FLT_MAX);
printf("float min = %??\n\n", FLT_MIN);
return 0;
}
为了使printf将各种数量显示为适当大小的十进制数字,我必须使用哪些说明符代替???
使用与这些类型的其他任何值相同的格式:
#include <float.h>
#include <stdio.h>
int main(void) {
printf("FLT_MAX = %g\n", FLT_MAX);
printf("DBL_MAX = %g\n", DBL_MAX);
printf("LDBL_MAX = %Lg\n", LDBL_MAX);
}
对于printf
等可变函数,类型float
的参数被提升为double
,这就是为什么您对两者使用相同格式的原因.
%f
使用不带指数的十进制表示法来打印浮点值,这将为您提供一个很长的字符串(对于大多数值来说是无关紧要的).
%e
强制使用指数.
%g
根据要打印的数字的大小使用%f
或%e
.
在我的系统上,上面显示了以下内容:
FLT_MAX = 3.40282e+38
DBL_MAX = 1.79769e+308
LDBL_MAX = 1.18973e+4932
正如Eric Postpischil在评论中指出的那样,以上内容仅显示这些值的近似值.您可以通过指定精度来打印更多数字(您需要的位数取决于类型的精度);例如,您可以将%g
替换为%.20g
.
或者,如果您的实现支持它,则C99可以根据需要以十六进制的精度打印浮点值的功能:
printf("FLT_MAX = %a\n", FLT_MAX);
printf("DBL_MAX = %a\n", DBL_MAX);
printf("LDBL_MAX = %La\n", LDBL_MAX);
但是结果并不像通常的十进制格式那样容易被人类理解:
FLT_MAX = 0x1.fffffep+127
DBL_MAX = 0x1.fffffffffffffp+1023
LDBL_MAX = 0xf.fffffffffffffffp+16380
(注意:main()
是一个过时的定义;请使用int main(void)
.)
For the following code,
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void) {
printf("double max = %??\n", DBL_MAX);
printf("double min = %??\n", DBL_MIN);
printf("double epsilon = %??\n", DBL_EPSILON);
printf("float epsilon = %??\n", FLT_EPSILON);
printf("float max = %??\n", FLT_MAX);
printf("float min = %??\n\n", FLT_MIN);
return 0;
}
what specifiers would I have to use in place of the ??'s in order for printf to display the various quantities as appropriately-sized decimal numbers?
Use the same format you'd use for any other values of those types:
#include <float.h>
#include <stdio.h>
int main(void) {
printf("FLT_MAX = %g\n", FLT_MAX);
printf("DBL_MAX = %g\n", DBL_MAX);
printf("LDBL_MAX = %Lg\n", LDBL_MAX);
}
Arguments of type float
are promoted to double
for variadic functions like printf
, which is why you use the same format for both.
%f
prints a floating-point value using decimal notation with no exponent, which will give you a very long string of (mostly insignificant) digits for very large values.
%e
forces the use of an exponent.
%g
uses either %f
or %e
, depending on the magnitude of the number being printed.
On my system, the above prints the following:
FLT_MAX = 3.40282e+38
DBL_MAX = 1.79769e+308
LDBL_MAX = 1.18973e+4932
As Eric Postpischil points out in a comment, the above prints only approximations of the values. You can print more digits by specifying a precision (the number of digits you'll need depends on the precision of the types); for example, you can replace %g
by %.20g
.
Or, if your implementation supports it, C99 added the ability to print floating-point values in hexadecimal with as much precision as necessary:
printf("FLT_MAX = %a\n", FLT_MAX);
printf("DBL_MAX = %a\n", DBL_MAX);
printf("LDBL_MAX = %La\n", LDBL_MAX);
But the result is not as easily human-readable as the usual decimal format:
FLT_MAX = 0x1.fffffep+127
DBL_MAX = 0x1.fffffffffffffp+1023
LDBL_MAX = 0xf.fffffffffffffffp+16380
(Note: main()
is an obsolescent definition; use int main(void)
instead.)
这篇关于如何在C中打印尽可能大的float和double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!