Checking if number is bigger than INT MAX

Discussion in 'C++' started by pocmatos@gmail.com, Dec 12, 2005.

  1. Guest

    Hi all,

    I'm doing parsing with flex and bison and I read numbers which are just
    a sequence of digits [0-9]+ and keep them in an int, however if the
    number is bigger than int, I should output error. What's the best way
    to check this?

    In the flex side I have:
    {numeral} { yylval.num = strtoul(yytext, NULL, 0);
    return NUMERAL; }

    Is there a straightforward way to check if the number in yytext is
    bigger than INT_MAX?

    Cheers,

    Paulo Matos
     
    , Dec 12, 2005
    #1
    1. Advertising

  2. wrote:
    > I'm doing parsing with flex and bison and I read numbers which are just
    > a sequence of digits [0-9]+ and keep them in an int, however if the
    > number is bigger than int, I should output error. What's the best way
    > to check this?
    >
    > In the flex side I have:
    > {numeral} { yylval.num = strtoul(yytext, NULL, 0);
    > return NUMERAL; }


    "flex" is off-topic, so without knowing what 'yylval' is or how its member
    'num' is declared, it's hard to say. However, since you're using the C
    function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
    long'. If it isn't, you need to supply more information.

    > Is there a straightforward way to check if the number in yytext is
    > bigger than INT_MAX?


    It is implementation-specific, most likely, but check 'yylval.num' against

    (unsigned long)INT_MAX

    instead. Whether it buys you anything or not, is for you to decide. You
    could also try to convert 'yytext' to an implementation-specific integral
    type that is known to accommodate a larger range than 'int' and check its
    value against (that_type)INT_MAX. I here used 'unsigned long' as that
    type, and your system may have something better.

    V
     
    Victor Bazarov, Dec 12, 2005
    #2
    1. Advertising

  3. Guest

    Victor Bazarov wrote:
    > wrote:
    > > I'm doing parsing with flex and bison and I read numbers which are just
    > > a sequence of digits [0-9]+ and keep them in an int, however if the
    > > number is bigger than int, I should output error. What's the best way
    > > to check this?
    > >
    > > In the flex side I have:
    > > {numeral} { yylval.num = strtoul(yytext, NULL, 0);
    > > return NUMERAL; }

    >
    > "flex" is off-topic, so without knowing what 'yylval' is or how its member
    > 'num' is declared, it's hard to say. However, since you're using the C
    > function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
    > long'. If it isn't, you need to supply more information.
    >
    > > Is there a straightforward way to check if the number in yytext is
    > > bigger than INT_MAX?

    >
    > It is implementation-specific, most likely, but check 'yylval.num' against
    >
    > (unsigned long)INT_MAX


    You might consider the "more C++y" static_cast<unsigned
    long>(std::numeric_limits<int>::max())
     
    , Dec 12, 2005
    #3
  4. Guest

    wrote:
    > Hi all,
    >
    > I'm doing parsing with flex and bison and I read numbers which are just
    > a sequence of digits [0-9]+ and keep them in an int, however if the
    > number is bigger than int, I should output error. What's the best way
    > to check this?


    Easy: Strip the leading '0' from the string, get INT_MAX as a string
    without leading zeroes. Compare lengths. If AsString(INT_MAX)
    is longer it will fit. If AsString(INT_MAX) is shorter, it won't. If
    the
    lengths are equal, you can do a string compare and it will fit if
    AsString(INT_MAX) < digit_sequence_without_leading_zeroes.

    HTH,
    Michiel Salters
     
    , Dec 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,220
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. Replies:
    7
    Views:
    611
    Dave Thompson
    Apr 18, 2005
  4. Replies:
    2
    Views:
    778
    Filip Larsen
    Apr 10, 2007
  5. Yannick Turgeon

    App getting bigger and bigger

    Yannick Turgeon, Oct 13, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    139
    Yannick Turgeon
    Oct 14, 2003
Loading...

Share This Page