Long double 1.#QNAN

Running the code below from Chapter 2 of "An Introduction to the C Programming Language and Software Design" by Tim Bailey works fine in C4Droid on Android but using CodeBlocks with the GCC compiler in Windows gives -1.#QNAN0e+000 for the upper limit of long double (LDBL_MAX). I understand that to mean "there's no such number", but presumably that means wrong coding and not "there's no limit" (I believe the correct answer is 1.797693e+308).

So, possible reasons I've checked:

  1. Typo: I don't see it (and I've also selected LDBL_MAX from the CodeBlocks editor).

  2. Type mismatch: I don't think so (%e should be fine).

  3. LDBL_MAX not included in float.h: It is included.

Any suggestions gratefully received

#include <stdio.h>
#include <limits.h>
#include <float.h>

int main (void)
{
    printf("Integer range:\t\t%d\t%d\n", INT_MIN, INT_MAX);
    printf("Long range:\t\t%ld\t%ld\n", LONG_MIN, LONG_MAX);
    printf("Float range:\t\t%e\t%e\n", FLT_MIN, FLT_MAX);
    printf("Double range:\t\t%e\t%e\n", DBL_MIN, DBL_MAX);
    printf("Long double range:\t%e\t%e\n", LDBL_MIN, LDBL_MAX);
    printf("Float-Double epsilon:\t%e\t%e\n", FLT_EPSILON, DBL_EPSILON);
}

1 answer

  • answered 2018-11-08 07:07 Some programmer dude

    If you read e.g. this printf reference you will see that all floating points format without any modifiers (including plain "%e") is for the type double.

    If you want to print the value of a long double you need the L modifier prefix, as in "%Le".

    Mismatching format specifier and type leads to undefined behavior.