Marine Jouanneau
Marine Jouanneau

Reputation: 75

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

#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 ?

Upvotes: 2

Views: 1382

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 223747

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

Upvotes: 2

dbush
dbush

Reputation: 224532

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 };

Upvotes: 0

Related Questions