473,803 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Direct computation of integer limits in K&R2?

Hello all,

In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.

Mar 11 '08
88 3806
CBFalconer said:
Ioannis Vranos wrote:
>>
... snip ...
>>
What is N869? My answer as C95 based. Actually since it is an
exercise of K&R2, it is a C90 question.

It is the last draft of C99 before publication, and the last
version available in text form.
He doesn't want one. In case you hadn't noticed, he's teaching a C90/C95
course.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 14 '08 #61
Displaying the limits using the macros is easy: the C89 Standard mandates
that the macros in <limits.hmust have the same type as an expression
that is an object of the corresponding type converted according to the
integral promotions (this excludes CHAR_BIT and MB_LEN_MAX). So this is
perfectly reasonable:

printf("char signed min = %d\n", SCHAR_MIN);
printf("signed int min = %d\n", INT_MIN);
printf("signed int max = %d\n", INT_MAX);
/* etc. */

For calculating them, Tondo & Gimpel suggest this in "The C Answer Book":

printf("signed char min = %d\n", ~(char)((unsign ed char) ~0 >1));
printf("signed char max = %d\n", (char)((unsigne d char) ~0 >1));
printf("unsigne d short min = %d\n", ~(short)((unsig ned short) ~0 >1));
printf("unsigne d short max = %d\n", (short)((unsign ed short) ~0 >1));

and so on.

Obviously, I don't know to what peer review T&G's Answer Book was subject,
but if their solutions are not correct, I would like to know.

--
Martin

Mar 14 '08 #62
ym******@gmail. com wrote:
What did you do in this code? Did you compute INT_MAX and
INT_MIN using INT_MAX and INT_MIN? Note that (unsigned)-1 / 2
may be out of range of int (by the way, C99 rationale says
that the committee was told that there actually was an
implementation where UINT_MAX + 1 was *four* times INT_MAX + 1).

How can this happen given that sizeof(int)== sizeof(unsigned it)?
Mar 14 '08 #63
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
ym******@gmail. com wrote:
>What did you do in this code? Did you compute INT_MAX and
INT_MIN using INT_MAX and INT_MIN? Note that (unsigned)-1 / 2
may be out of range of int (by the way, C99 rationale says
that the committee was told that there actually was an
implementati on where UINT_MAX + 1 was *four* times INT_MAX + 1).


How can this happen given that sizeof(int)== sizeof(unsigned it)?
Not all the bits in int are required to be value or sign bits (some
may be padding bits). And some of the bits that are value bits in an
unsigned int are allowed to be padding bits in the signed int.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Mar 14 '08 #64
Ioannis Vranos wrote:
>
.... snip ...
>
Well we can see if INT_MIN==-INT_MAX-1 or not. And then use my
code if this is the case, for the else part perhaps you may
provide some solution. :-)
If not, you have discovered undefined behaviour.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '08 #65
Martin <m@b.cwrites:
Displaying the limits using the macros is easy:
Agreed, so I've snipped...
For calculating them, Tondo & Gimpel suggest this in "The C Answer Book":

printf("signed char min = %d\n", ~(char)((unsign ed char) ~0 >1));
printf("signed char max = %d\n", (char)((unsigne d char) ~0 >1));
printf("unsigne d short min = %d\n", ~(short)((unsig ned short) ~0 >1));
printf("unsigne d short max = %d\n", (short)((unsign ed short) ~0 >1));

and so on.

Obviously, I don't know to what peer review T&G's Answer Book was
subject, but if their solutions are not correct, I would like to
know.
These can't work on sign & magnitude systems since the min. and max.
values aren't the (bit-wise) complement of each other. On ones'
complement systems, (unsigned char)~0 should be zero, I think -- the
conversion is based on the value of ~0 not the bit pattern. On twos'
complement systems, it is possible that both signed and unsigned short
have the same number of value bits. On such a system, the expression
will print too small a maximum.

--
Ben.
Mar 14 '08 #66
On Tue, 11 Mar 2008 23:41:33 -0500, CBFalconer wrote:
Harald van D?k wrote:
>santosh wrote:
>>In K&R2 one exercise asks the reader to compute and print the limits
for the basic integer types. This is trivial for unsigned types. But
is it possible for signed types without invoking undefined behaviour
triggered by overflow? Remember that the constants in limits.h cannot
be used.

#include <stdio.h>
int main(void) {
unsigned u = -1;
int i;
while ((i = u) < 0 || i != u)
u = u >1;
printf("INT_MAX == %u\n", u);
}

This is not guaranteed to work in C99, where the conversion of an
out-of- range integer may raise a signal, but it's valid C90, since the
result of the conversion must be a valid int, and therefore between
INT_MIN and INT_MAX.

This CAN'T work everywhere.
Yes, it can.
The u = -1 statement is legal, and results
in UINT_MAX value. However the first i = u statement always overruns
the INT_MAX value for i, unless the system has INT_MAX defined to be
equal to UINT_MAX. Very rare. So the result of that statement is
implementation defined.
Of course. And any of the permissible implementation-defined values of i
will result in the intended behaviour.

As long as u INT_MAX, then i cannot possibly be equal to u. It could be
negative, or it could be zero, or it could be positive. If it is
negative, then the comparison to 0 continues the loop. If it is not
negative, then i != u, because i <= INT_MAX, and u INT_MAX, so the loop
continues.

As soon as u <= INT_MAX, then i must be equal to u, so the loop exits.
Mar 14 '08 #67
On Fri, 14 Mar 2008 03:51:29 +0200, Ioannis Vranos wrote:
if (INT_MIN== -INT_MAX -1)
On systems where the condition doesn't hold, -INT_MAX -1 overflows.
Mar 14 '08 #68
Micah Cowan wrote:
Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
>ym******@gmail. com wrote:
>>What did you do in this code? Did you compute INT_MAX and
INT_MIN using INT_MAX and INT_MIN? Note that (unsigned)-1 / 2
may be out of range of int (by the way, C99 rationale says
that the committee was told that there actually was an
implementatio n where UINT_MAX + 1 was *four* times INT_MAX + 1).

How can this happen given that sizeof(int)== sizeof(unsigned it)?

Not all the bits in int are required to be value or sign bits (some
may be padding bits). And some of the bits that are value bits in an
unsigned int are allowed to be padding bits in the signed int.
OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?
Mar 14 '08 #69
Ioannis Vranos wrote:
Micah Cowan wrote:
>Ioannis Vranos <iv*****@nospam .no.spamfreemai l.grwrites:
>>ym******@gmail. com wrote:
What did you do in this code? Did you compute INT_MAX and
INT_MIN using INT_MAX and INT_MIN? Note that (unsigned)-1 / 2
may be out of range of int (by the way, C99 rationale says
that the committee was told that there actually was an
implementati on where UINT_MAX + 1 was *four* times INT_MAX + 1).

How can this happen given that sizeof(int)== sizeof(unsigned it)?

Not all the bits in int are required to be value or sign bits (some
may be padding bits). And some of the bits that are value bits in an
unsigned int are allowed to be padding bits in the signed int.

OK it is allowed, but is there any existing system where the value
ranges of signed int and unsigned int are different?

With "value ranges" above I mean the total values of each type.
Mar 14 '08 #70

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.