[英]How to define constant in floating point double precision in c89
I have code with a constant declared in a representation that is not valid in c89 (compiler option on historic project).我的代码中声明的常量在 c89 中无效(历史项目的编译器选项)。
#define K_MAX_KCG_REAL 0x1.FFFFFFFFFFFFFp1023
I am looking for a solution valid in c89我正在寻找在 c89 中有效的解决方案
I have tried我试过了
#define K_MAX_KCG_REAL 0x7FEFFFFFFFFFFFFF
but is is interpreted as an integer with a float value of approx.但被解释为一个整数,浮点值约为。 9.22e18.
9.22e18。 Far from 1.79e308 that I need.
远不是我需要的 1.79e308。 What is the best way to declare a coonstant with max value for double precision ?
为双精度声明具有最大值的常数的最佳方法是什么?
The number you are looking for is 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.
您正在查找的号码是
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.
If DBL_MAX
is defined in <float.h>
, you should use that.如果
DBL_MAX
在<float.h>
定义,你应该使用它。
With many compilers, 1.7976931348623157e308
would suffice.对于许多编译器,
1.7976931348623157e308
就足够了。
If the compiler fails to parse those correctly, you could try (9007199254740991. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 2048.)
如果编译器不能正确地分析这些,你可以尝试
(9007199254740991. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 1073741824. * 2048.)
You'll need to create a union containing a unsigned long long
and a double
and initialize the former.您需要创建一个包含
unsigned long long
和double
并初始化前者。
const union {
unsigned long long i;
double d;
} K_MAX_KCG_REAL = { 0x7FEFFFFFFFFFFFFF };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.