Thread: problem while using strod

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    problem while using strod

    How can i check if there was any overflow or underflow when using strtod..

    i googled about it and found that some HUGE_VAL is set accordingly...
    but when i used
    Code:
    double temp = atof(yytext);
    	if (temp == HUGE_VAL)
    on compilation, i got an error: error C2065: 'HUGE_VAL' : undeclared identifier


    i am right now compiling the code on a windows machine, but my code will be finally sun on both windows and linux..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, atof() is different in this sense than strtod(). atof() is not guaranteed to catch bad input at all.

    The strtod() function specifies that it will detect overflows.

    HUGE_VAL is supposed to be defined in <math.h> - do you include that file?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    thanks .... i didn't include math.h initially.. :-|

    there is another thing that i need to know...is HUGE_VAL macro part of C89 standards..
    if it is not then how can we have a check for huge values while writing a code strictly conforming to C89...

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    and i have written atof in my first post by mistake.. i am using strtod only..

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ISO9899:1999, section 7.12.3:
    The macro
    HUGE_VAL
    expands to a positive double constant expression, not necessarily representable as a
    float. The macros
    HUGE_VALF
    HUGE_VALL
    are respectively float and long double analogs of HUGE_VAL
    Sorry some of the nice formatting in the spec itself got lost there..

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    but is HUGE_VAL part of C89 standard...
    if i am not wrong you have quoted using the C99 standard..

    bear me if i am wrong :-)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doh! Sorry, the caffeine from the Coke obviously hasn't quite worked it's way in yet.

    I'll see if I can find out. FWIW, gcc -ansi -std=c89 -pedantic -Wall compiles this:
    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main() 
    { 
       printf("%f", HUGE_VAL); 
       return 0; 
    }
    That's using gcc-mingw 3.4.2.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    hehe...
    i checked ...HUGE_VAL is there in the C89 draft...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by technosavvy View Post
    hehe...
    i checked ...HUGE_VAL is there in the C89 draft...
    Good. And if it's in the draft, then it will be in the ratified version as well, as I can't see why it would be a contentious issue that got removed from the draft.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM