I saw this code somewhere
printf("DBL_MAX : %g\n", (double) DBL_MAX);
Is the cast necessary? I cannot find anything in the standard which specifies DBL_MAX must actually have (or promote to) type double.
DBL_MAX is specified to be a macro, so in itself, does not have a type.
The requirement from the 1999 C standard is that DBL_MAX must expand to a constant value that is at least 1E+37, but nothing is said about its actual type. Since it represents the implementation-defined maximum value that a double can represent, logic would suggest the expansion is a value that can be converted to a double without any change (e.g. loss of precision).
This logic would allow but not require DBL_MAX to be of type double. It could also be of type long double (since it is also required that a long double can exactly represent all values that a double can).
Practically, it is difficult to envisage an implementation which has DBL_MAX expand to a constant that is not of type double, and I certainly have never heard mention of one. But that does not mean it is required. printf("DBL_MAX : %g\n", (double) DBL_MAX) provides a conservative (in the sense of making provision for a possibility that is allowed, even if it might not occur) guarantee that a value of type double is passed to printf().
I don't believe there's anything in the standard that really says that DBL_MAX is an expression of type double.
The description in N1570 5.2.4.2.2p12, which specifies FLT_MAX, DBL_MAX, and LDBL_MAX, is:
The values given in the following list shall be replaced by constant expressions with implementation-defined values that are greater than or equal to those shown:
(all three must be at least 1E+37).
Just as a matter of common sense, I can think of no good reason for the expression to which the FLT_MAX, DBL_MAX, and LDBL_MAX macros expand not to be of types float, double, and long double, respectively. I suspect that the authors of the standard simply assumed that they would be of the expected type. I personally would be comfortable omitting the cast and using DBL_MAX rather than (double)DBL_MAX.
If an implementation used:
#define DBL_MAX 1.79769313486231570815e+308L
giving it a type of long double, it would be counterintuitive, but I don't see any requirement in the Standard that it would violate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With