Printing long double precision

Dec 10, 2009 at 2:17am
The maximum value for the Microsoft Visual C++ 6.0 compiler is 1.797693e308. I would like to print this. Could someone tell me why im getting an output of -1.#QNAN0e+000?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
int c = 1.797693*pow(10,308);
printf("test: %8.6Le \n\n", c);
system("pause");
return 0;
}
Dec 10, 2009 at 2:36am
What exactly is an Le?

Also, you are storing it into an int, not a long double, which means printf() is reading garbage memory.

And don't use system()...
Dec 10, 2009 at 2:44am
%Le is a conversion specifier for long double output statements. If I switch to double main(void) I get the desired output.

However, I still get an output of -1.#QNAN0e+000 if I instead use

printf("long double maximum: %8.6Le \n\n", LDBL_MAX);

LDBL_MAX is supposed to retun the value of 1.797693e308.

Im new to C++. I've heard that I shouldnt use system("pause"), but I dont know what command I should use in its place. Ive heard cin.get() is better. Do I need a a preprocessor directive to run cin.get()? When I put it in the place of system("pause"), it gives me an error.


Dec 10, 2009 at 2:48am
C++? It looks like you are using C from your header files. As a result, you don't have cin to use .get() on. I forget what the recommended procedure is for C...
Dec 10, 2009 at 2:51am
Im using Dev-C++ 4.9.9.2, but Im using a C textbook.
Dec 10, 2009 at 2:56am
In any case, you are storing the maximum size of a double into a int. This truncates it. After that, you tell printf to read from the int's memory as if it were a double (as firedraco said), which means printf is reading garbage data.
Dec 10, 2009 at 2:59am
Yes I understand, and I switched to double; however, I still get an output of -1.#QNAN0e+000.
Dec 10, 2009 at 3:09am
Perhaps there is a precision error that is introduced when you calculate the value manually?
Dec 10, 2009 at 3:26am
I did not make LDBL_MAX myself. Apparently it stores the value of the maximum value for the Microsoft Visual C++ 6.0 compiler (1.797693e308). If I make up a variable, give it the value of 1.797693e308 and print the variable I get the desired output. Its no big deal. Its just that my textbook gave this code in an example, and when I tested it out, it didn't really work.
Dec 10, 2009 at 3:42am
I know you didn't make it up. What I was saying is using pow() to calculate a value then multiplying is probably causing a loss of precision as opposed to just putting in the exact value via hard-coding it.
Dec 10, 2009 at 4:04am
Ah, I see. Thanks.
Topic archived. No new replies allowed.