Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Direct computation of integer limits in K&R2?

Reply
Thread Tools

Direct computation of integer limits in K&R2?

 
 
santosh
Guest
Posts: n/a
 
      03-11-2008
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.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-11-2008
santosh wrote:
> 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.
>

Isn't it possible to calculate this based on the unsigned types of the
same size?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-11-2008
Ian Collins wrote:

> santosh wrote:
>> 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.
>>

> Isn't it possible to calculate this based on the unsigned types of the
> same size?


Won't this require knowledge of the encoding used, whether twos
complement or sign and magnitude etc?

 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      03-11-2008
On Wed, 12 Mar 2008 03:07:48 +0530, santosh wrote:
> 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.


#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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-11-2008
santosh wrote:
> Ian Collins wrote:
>
>> santosh wrote:
>>> 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.
>>>

>> Isn't it possible to calculate this based on the unsigned types of the
>> same size?

>
> Won't this require knowledge of the encoding used, whether twos
> complement or sign and magnitude etc?
>

I think so, I should have added that.

--
Ian Collins.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-11-2008
santosh <(E-Mail Removed)> 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.


Yes. Unlike C99, unsigned to signed integer conversion
is implementation defined without the possibility of
raising a signal. So...

<http://groups.google.com/group/comp.lang.c/msg/ffe17c645660b76c>

INT_MIN isn't computed per se, rather it's derived by
determining the representation for negative ints. [I
know pete posted some very simple constant expressions,
though it was some time ago.]

--
Peter
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-11-2008
Harald van D?k wrote:

> On Wed, 12 Mar 2008 03:07:48 +0530, santosh wrote:
>> 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.

>
> #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.


Thanks. What about the minima?

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-11-2008
Peter Nilsson wrote:

> santosh <(E-Mail Removed)> 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.

>
> Yes. Unlike C99, unsigned to signed integer conversion
> is implementation defined without the possibility of
> raising a signal. So...
>
> <http://groups.google.com/group/comp.lang.c/msg/ffe17c645660b76c>
>
> INT_MIN isn't computed per se, rather it's derived by
> determining the representation for negative ints. [I
> know pete posted some very simple constant expressions,
> though it was some time ago.]


Would you say that this exercise is overly complex for that point in
K&R2?

 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      03-11-2008
On Wed, 12 Mar 2008 03:29:53 +0530, santosh wrote:
> Harald van D?k wrote:
>> On Wed, 12 Mar 2008 03:07:48 +0530, santosh wrote:
>>> 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.

>>
>> #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.

>
> Thanks. What about the minima?


Up to INT_MIN, you can use this same idea, except start from LONG_MIN
instead of UINT_MAX. For LONG_MIN, I would cheat with
strtol("-999999999", 0, 0)
adding 9s until a range error is returned.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-11-2008
Harald van D?k wrote:

> On Wed, 12 Mar 2008 03:29:53 +0530, santosh wrote:
>> Harald van D?k wrote:
>>> On Wed, 12 Mar 2008 03:07:48 +0530, santosh wrote:
>>>> 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.
>>>
>>> #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.

>>
>> Thanks. What about the minima?

>
> Up to INT_MIN, you can use this same idea, except start from LONG_MIN
> instead of UINT_MAX. For LONG_MIN, I would cheat with
> strtol("-999999999", 0, 0)
> adding 9s until a range error is returned.


Okay. I for one am glad that limits.h exists.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function for direct conversion integer > slv aleksazr@gmail.com VHDL 2 12-25-2012 07:22 PM
where can I find good samples for efficient computation of matrix multiplication? walala VHDL 2 03-24-2010 10:06 AM
GPS : Basic pseudo-distance computation le Cl? VHDL 15 04-11-2009 09:22 AM
"Symbolic computation" Jesper Sahner Java 3 12-06-2004 10:41 AM



Advertisments