469,270 Members | 1,239 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Checking if number is bigger than INT MAX

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 '05 #1
3 7881
po******@gmail.com 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
Dec 12 '05 #2

Victor Bazarov wrote:
po******@gmail.com 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 '05 #3
po******@gmail.com 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 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by don | last post: by
7 posts views Thread by Jim Cook | last post: by
2 posts views Thread by Marlene Stebbins | last post: by
21 posts views Thread by jacob navia | last post: by
9 posts views Thread by LeAnne | last post: by
4 posts views Thread by Patient Guy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.