Thread: Printing long double gives zero

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    28

    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. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    800
    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.

    As for the remainder of your post, not enough information to answer your questions.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    844
    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. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    28
    Quote Originally Posted by rstanley View Post
    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.

    As for the remainder of your post, not enough information to answer your questions.
    I'm using GNU GCC compiler on Windows 10, running the same code you wrote in my computer gives 0.0000.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    28
    Quote Originally Posted by flp1969 View Post
    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. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    800
    Quote Originally Posted by amohammed63 View Post
    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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,574
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    844
    Quote Originally Posted by amohammed63 View Post
    -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 subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-07-2015, 03:28 PM
  2. problem while printing unsigned long long int
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 03-08-2012, 04:44 AM
  3. number bigger than long long double
    By suryak in forum C Programming
    Replies: 9
    Last Post: 08-18-2011, 02:02 PM
  4. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  5. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM

Tags for this Thread