Thread: unable to use long double

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    unable to use long double

    Hello,
    I'm having trouble using the long double type. So I wrote a small c program to output the max number representable by long double, and the result was that on my computer the max number for long double is approx zero! (see the attached .PNG) file. I have a Lenovo P71 computer and I'm using gcc. Does anyone have an idea why this is happening? Is it connected to my hardware?
    Regards,
    bostjanv
    Attached Images Attached Images unable to use long double-longdouble-png 
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    You should use the corresponding format specifiers for the values that you are trying to print, e.g., the type of the result of sizeof is an unsigned integer, so you should either use %zu, or if you must compile with respect to an older version of the C standard, use %u then cast the result of sizeof to unsigned int. Likewise, for printing long double you should use %Le instead of %e:
    Code:
    #include <float.h>
    #include <stdio.h>
    
    int main(void) {
        long double r;
    
        printf("sizeof(float) %zu\nsizeof(double) %zu\nsizeof(long double) %zu\n",
            sizeof(float),
            sizeof(double),
            sizeof(long double));
        printf("FLT_MAX %e\nDBL_MAX %e\nLDBL_MAX %Le\n",
            FLT_MAX,
            DBL_MAX,
            LDBL_MAX);
    
        r = 1.1L + 1.2L;
        printf("r=1.1L+1.2L;\nr=%Le\n", r);
    }
    Notice that instead of using %s as a placeholder and then supplying the text as arguments to printf, I embedded the text directly in the format string. This is the usual approach for readability.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Thanks for your reply, and I appreciate your style suggestions; however, after making the changes you suggested I am still getting 3.302445e-317 as the value of LDBL_MAX, and the same as the value of r after the assignment r=1.1L+1.2L; (?)
    Regards,
    bostjanv

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    c - printf and long double - Stack Overflow
    Type long double | Microsoft Docs
    Gcc on windows uses the Microsoft run time library, which doesn't understand true long double data types.
    You have to make it use the alternative mingw library.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Thanks. It turns out I needed two changes: gcc needs to be used with the flag -D__USE_MINGW_ANSI_STIO, and the program needs the macro
    #define printf __mingw_printf

    Regards,
    bostjanv

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    From memory "-D__USE_MINGW_ANSI_STIO" should be "-D__USE_MINGW_ANSI_STDIO" that is with a D added.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    You are right; but the thing works even without the "D". I am not sure how gcc recognizes its options.
    Regards,
    bostjanv

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-07-2015, 03:28 PM
  2. number bigger than long long double
    By suryak in forum C Programming
    Replies: 9
    Last Post: 08-18-2011, 02:02 PM
  3. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  4. 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
  5. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM

Tags for this Thread