2

I have code with a constant declared in a representation that is not valid in c89 (compiler option on historic project).

#define K_MAX_KCG_REAL 0x1.FFFFFFFFFFFFFp1023

I am looking for a solution valid in 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. Far from 1.79e308 that I need. What is the best way to declare a coonstant with max value for double precision ?

2 Answers2

2

The number you are looking for is 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.

If DBL_MAX is defined in <float.h>, you should use that.

With many compilers, 1.7976931348623157e308 would suffice.

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.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

You'll need to create a union containing a unsigned long long and a double and initialize the former.

const union {
    unsigned long long i;
    double d;
} K_MAX_KCG_REAL = { 0x7FEFFFFFFFFFFFFF };
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    `unsigned long long` would be a non-standard, extended type in c89/c90 though. – Ian Abbott Jul 08 '19 at 13:04
  • 1
    I don't think `unsigned long long` is standard per C89. It's mentioned as an extension under [**A.6.5 Common extensions**](https://port70.net/~nsz/c/c89/c89-draft.html). – Andrew Henle Jul 08 '19 at 13:06