# Thread: Printing long double gives zero

1. ## Printing long double gives zero

Hello,

I have a project which is performing addition, multiplication and subtraction on a file of equations. Adding or subtracting a big number of equations and substituting a value of x in the result works correctly, I mean it's in the range of double format. But, multiplying 10 equations and substituting x = 1 in the result gives a number in millions, if x = 5, the value is (-1.#IND) and I don't know what does it mean. So, I was looking to use long double so that It may solve the problem, but when I code the following segment, the output is 0.0000;
Code:
```long double num = 50;
printf("%Lf", num);```
So, I'm facing two problems, the first how can I do my project works for large values of X, or large number of equations, what format should I use? The second is, if long double is a solution, how can I use it? 2. What compiler are you using?

Your code works correctly compiling with gcc on Debian Linux.
Code:
```#include <stdio.h>

int main(void)
{
long double num = 50;
printf("%Lf\n", num);

return 0;
}```
Please provide a small program that can compile and run, that demonstrates the result you describe. 3. What do you mean "large values"? double supports values between +/-1.78769*10³⁰⁸ (large enough for me).
Using long double is always a overkill (large object representation in memory and no support from SIMD).

If you really need to deal with very "large values", beyond 'double' or 'long double' ranges, consider using multiprecision arithmetic libraries as libgmp. 4. Originally Posted by rstanley What compiler are you using?

Your code works correctly compiling with gcc on Debian Linux.
Code:
```#include <stdio.h>

int main(void)
{
long double num = 50;
printf("%Lf\n", num);

return 0;
}```
Please provide a small program that can compile and run, that demonstrates the result you describe.

I'm using GNU GCC compiler on Windows 10, running the same code you wrote in my computer gives 0.0000. 5. Originally Posted by flp1969 What do you mean "large values"? double supports values between +/-1.78769*10³⁰⁸ (large enough for me).
Using long double is always a overkill (large object representation in memory and no support from SIMD).

If you really need to deal with very "large values", beyond 'double' or 'long double' ranges, consider using multiprecision arithmetic libraries as libgmp.
-5579207423999930400.0000 this is the value of multiplying 16 equations and substitute x = 5. So that why I'm looking for long double. 6. Originally Posted by amohammed63 I'm using GNU GCC compiler on Windows 10, running the same code you wrote in my computer gives 0.0000.
Well your first issue is compiling under Windows 10! ;^)

But seriously, what version of gcc? "gcc --version"

How did you install gcc? Mingw64, cygwin, other?

What compile command? Did you turn on warnings? 7. cant print correctly a long double in C - Stack Overflow
The problem stems from MinGW using the Microsoft runtime library (the code that implements printf).
This is all well and good until you get to long double, at which point the compiler and runtime no longer agree on what a long double should look like.

Try this
Code:
```int main ( ) {
printf("Long double size=%zd\n", sizeof(long double));
}```
If your MinGW prints something other than say 8 (for a 64-bit long double), then the whole internal representation is off compared to what the Microsoft runtime library expects.

The fix would seem to be
gcc -D__USE_MINGW_ANSI_STDIO prog.c 8. Originally Posted by amohammed63 -5579207423999930400.0000 this is the value of multiplying 16 equations and substitute x = 5. So that why I'm looking for long double.
Ok. At this magnitude a long double has 1 ULP = 0.5. It means the imprecision is +/- 0.25. As for a double, 1 ULP = 1024 giving an imprecision of +/- 512. To illustrate, 5579207423999930400.0 cannot be represented exactly in double, the nearest value is 5579207423999930368.0.

If you really need that much precision (more than 19 decimal algarisms) you have 2 choices:

To use a GCC extension, the type __float128, which gives us 113 bits of precision. At the same magintude of previous value, 1 ULP = 8.88178419700125*10⁻¹⁶ giving an error of +/- 4.44089209850062616169452667236328125*10⁻¹⁶, but you have to use libquadmath library to use the appropriate math functions and the quadmath_printf() to show the result.

To use __float128 is really slow, since all calculations are made by software, with no floating point acceleration like the math co-processor (80x87) or SIMD.

The other way is to use a multiple precision arithmetic library such as libgmp (even slower and consume too much memory, sometimes -- and the code is more complex). Popular pages Recent additions double, equations, long, number, works 