Checking if number is bigger than INT MAX

P

pocmatos

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
 
V

Victor Bazarov

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
 
R

roberts.noah

Victor said:
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())
 
M

Michiel.Salters

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top