Thread: MinGW, C99 + strtod

  1. #1
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Question MinGW, C99 + strtod

    Apologies up front for not finding this already. I'd Googled earlier and didn't find what I was looking for in a reasonable amount of time. And then I've had many distractions throughout this time too.

    The basic thing is this: with Code::Blocks' setup with MinGW, I tried to do this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <errno.h>
    
    int mygetd(double *result);
    
    int main(void)
    {
       double value = -12.3;
       do
       {
          fputs("Enter a floating-point number: ", stdout);
          fflush(stdout);
       } while ( !mygetd(&value) );
       printf("value = %g\n", value);
       return 0;
    }
    
    /**
     * Attempt to read a double value from the stdin.
     * @param  result  pointer to a double into which the result will be written if
     *                 a double is successfully converted
     * @return
     *    0 - failed to convert all text into a valid double, or
     *    1 - successfully obtained a double and placed its value in *result
     */
    int mygetd(double *result)
    {
       char *end, buff [ 32 ];
       fgets(buff, sizeof buff, stdin);
       errno = 0;
       *result = strtod(buff, &end);
       if ( errno )
       {
          perror("strtod");
       }
       return !isspace(*buff) && (*end == '\n' || *end == '\0');
    }
    
    /* my output
    Enter a floating-point number: 0X1P-23F
    Enter a floating-point number: 0X1.fffffffffffffP1023
    Enter a floating-point number: 1.7976931348623157E+308
    value = 1.79769e+308
    */
    It ain't takin' too kindly to some C99-okay input, and I've been trying to see if that's a bug with MinGW's standard library implementation, or if I need to further link with specific libraries.

    I'll continue looking, but if anyone else cares to take the time to lend me a hand, I'll be grateful.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's probably not important, but that looks like a NAN to me. This is what n1124 says:
    NAN or NAN(n-char-sequence ),ignoring case in the NAN part, where:
    opt
    n-char-sequence:
    digit
    nondigit
    n-char-sequence digit
    n-char-sequence nondigit
    The subject sequence is defined as the longest initial subsequence of the input string,
    starting with the first non-white-space character,that is of the expected form.The subject
    sequence contains no characters if the input string is not of the expected form.
    Another implementation admits that strtod and friends do not understand NAN syntax, though the page is three years old. This is probably the same case for yours.

    HTH.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Doesn't MinGW take the same approach as dev-c++ (which uses MinGW) in that it adopts the msvcrt standard library. So you get all the warts and baggage which that gives you.

    ISTR that to print a long long for example, you have to use the %I64 or whatever it is rather than the standard "%lld"
    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Salem
    Doesn't MinGW take the same approach as dev-c++ (which uses MinGW) in that it adopts the msvcrt standard library. So you get all the warts and baggage which that gives you.

    ISTR that to print a long long for example, you have to use the %I64 or whatever it is rather than the standard "%lld"
    Ah, yes. That's probably the direction I need to head for this issue.
    Code:
    int main(void)
    {
       double value = 0X1P-23F;
    #if 0
       do
       {
          fputs("Enter a floating-point number: ", stdout);
          fflush(stdout);
       } while ( !mygetd(&value) );
    #endif
       printf("value = %g\n", value);
       return 0;
    }
    
    /* my output
    J:\Forum\StdC.exe 
    value = 1.19209e-007
    */
    The compiler seems fine with this syntax.

    Thanks for the memory jog, I forgot about the msvcrt tie.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C89 or C99
    By talin in forum C Programming
    Replies: 6
    Last Post: 05-26-2008, 12:45 PM
  2. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  3. compiling mingw to enable timeSetEvent
    By underthesun in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2005, 06:00 PM
  4. My first game
    By homeyg in forum Game Programming
    Replies: 20
    Last Post: 12-22-2004, 05:25 PM
  5. Help with strtod and fgets..
    By Gugge in forum C Programming
    Replies: 2
    Last Post: 05-15-2002, 02:21 PM