typedef unsigned int my_type;
#define MY_TYPE_MAX ((my_type)(-1))
The goal is to be able to change the type of my_type and have
MY_TYPE_MAX automatically be correct. I am assuming that my_type will
be unsigned. Any better ideas? Thanks.
-Kevin
So long as my_type is unsigned, you're safe.
-- Mat.
It's fine. A pair of parens is redundent:
#define MY_TYPE_MAX ((my_type)-1)
How about a generic:
#define MY_MAX_OF(T) ((T)-1)
--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
But how can I find out the maximum value of a signed type?
I have thought about that and found no really correct solution.
There is no safe way to compute that because signed integer overflow
invokes undefined behavior.
Two theoretically unsafe methods that might work in the Real World are
simply taking Utype_MAX / 2, or incrementing a variable until it wraps
around, then decrementing it by one.
The best way, however, is to provide wrappers for the appropriate
<limits.h> macros.
Gergo
--
Weekend, where are you?
All defined in limits.h
--
Chuck F (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
>"Mathew Hendry" <mathew...@hotmail.com> wrote in message
>news:cq5gduc4n2f6roamu...@4ax.com...
>
>> On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
>> wrote:
>>
>> >Is the following code safe?
>> >
>> >typedef unsigned int my_type;
>> >#define MY_TYPE_MAX ((my_type)(-1))
>>
>> So long as my_type is unsigned, you're safe.
>
>But how can I find out the maximum value of a signed type?
>I have thought about that and found no really correct solution.
There isn't one. You could assume that all types of a given size have
the same range, and choose constants from <limits.h> accordingly, but
I'll wager there are machines where that wouldn't work.
-- Mat.
It's guaranteed to be one less than a power of two. You can try to
store such values in an object of type my_type and then compare the
value stored in the object with the one that was assigned to it. The
largest value that compares equal is the one you're looking for.
This approach is safe in C89 but *theoretically* unsafe in C99. For
practical reasons, you can safely expect it to work in C99, too.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan...@ifh.de
>Martin F. <RFr...@t-online.de> wrote:
>> "Mathew Hendry" <mathew...@hotmail.com> wrote in message
>> news:cq5gduc4n2f6roamu...@4ax.com...
>> > On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
>> > wrote:
>> > >Is the following code safe?
>> > >
>> > >typedef unsigned int my_type;
>> > >#define MY_TYPE_MAX ((my_type)(-1))
>> >
>> > So long as my_type is unsigned, you're safe.
>>
>> But how can I find out the maximum value of a signed type?
>> I have thought about that and found no really correct solution.
>
>There is no safe way to compute that because signed integer overflow
>invokes undefined behavior.
But you don't need to generate signed integer overflow in the process
of computing it. It can be done exclusively with assignments, which
are safe in C89.
>"Martin F." wrote:
>>
>> "Mathew Hendry" <mathew...@hotmail.com> wrote in message
>> news:cq5gduc4n2f6roamu...@4ax.com...
>> > On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
>> > wrote:
>> >
>> > >Is the following code safe?
>> > >
>> > >typedef unsigned int my_type;
>> > >#define MY_TYPE_MAX ((my_type)(-1))
>> > >
>> > >The goal is to be able to change the type of my_type and have
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> > >MY_TYPE_MAX automatically be correct. I am assuming that my_type will
^^^^^^^^^^^^=============^^^^^^^^^^^
>> > >be unsigned. Any better ideas? Thanks.
>> >
>> > So long as my_type is unsigned, you're safe.
>>
>> But how can I find out the maximum value of a signed type?
>> I have thought about that and found no really correct solution.
>
>All defined in limits.h
It doesn't hurt to engage your brain before your fingers.
<limits.h> is of little help if the actual type definition is not
known when the code is written. Think about a C89 program that needs
to be able to figure out PTRDIFF_MAX.
Not quite. What, for example, is the maximum value of
a `time_t'? Of a `clock_t'?
>In 'comp.lang.c', good...@bridgernet.com (Kevin Goodsell) wrote in
>news:2fa5ba58.02050...@posting.google.com:
>
>> Is the following code safe?
>>
>> typedef unsigned int my_type;
>> #define MY_TYPE_MAX ((my_type)(-1))
>>
>How about a generic:
>
>#define MY_MAX_OF(T) ((T)-1)
Great as long as you're cautious. One can be too easily misled into
thinking that it's a safe "operator" by now and write code like
type_t var;
. . .
size_t n = MY_MAX_OF(var);
I don't believe the standard requires a two's complement internal
format. If this were on a one's complement 8-bit machine, negative one
would be represented as binary 10000001, far from the max unsigned
value.
--
jc...@strobedata.com Live Honourably 4/1 - 4/3 + 4/5 - 4/7 + . . .
2002 Thu: Feb/last 4/4 6/6 8/8/ 10/10 12/12 9/5 5/9 7/11 11/7 3/14
Strobe Data Inc. home page http://www.strobedata.com
My home page O- http://jcook.net
Hardly. You described sign/magnitude. It would be 11111110.
However the conversion to unsigned range has to follow 2's
complement rules, and so is not affected by the underlying
arithmetic system.
How do you generate the numbers to be assigned? How are these numbers
converted if they are not in the range representable by the target? Is
this different in C99?
Gergo
--
Everyone is a genius. It's just that some people are too stupid to
realize it.
Since `my_type' is known to be an unsigned type, none
of this makes any difference. Remember, a cast operator
*converts* the *value* of its operand from one type to
another; it does *not* say "Interpret this bag of bits as
if they were of a specified type." In the expression
above, the *value* negative one, no matter what bit pattern
it has, is *converted* to `my_type', and by the rules of
unsigned arithmetic this necessarily produces the maximum
possible `my_type' value consisting entirely of 1-bits.
> > format. If this were on a one's complement 8-bit machine, negative
> > one would be represented as binary 10000001, far from the max
> Hardly. You described sign/magnitude. It would be 11111110.
> However the conversion to unsigned range has to follow 2's
> complement rules, and so is not affected by the underlying
Arghhhh. I can't believe I made a mistake of that ... er ... magnitude.
"The C Programming Language, Kernighan and Ritchie" says:
"A6.2 Integral Conversions
"Any integer is converted to a given unsigned type by finding the
smallest non-negative value that is congruent to that integer, modulo
one more than the largest value that can be represented in the unsigned
type."
So, to be nitpicky (a tenuous position for me to take at this time, I
grant), it's not following 2's complement rules, but simple arithmetic.
It amounts to producing the proper answer, though.
A negative one, modulo one more than the largest value ... will clearly
give the largest value. By definition.
I stand corrected on what a type cast does.
I believe the conversion of unsigned to signed is implementation
defined if the value is not representable for the target type. But
since positive [signed] numbers must have the same representation as
their unsigned counterparts [except possibly 0!] I believe the
following might work for signed "mystery" integers:
mystery mystery_max(void)
{
uintmax_t prev, curr; /* or unsigned long for C90 say? */
mystery best = 0;
for (prev = 63; (curr = 2*prev + 1) != prev; prev = curr)
if ((mystery) curr > best)
best = curr;
return best;
}
--
Peter
>Dan Pop <Dan...@ifh.de> wrote:
>> In <slrnadgh0l...@hold.otthon.at> Gergo Barany <ge...@tud.at> writes:
>> >Martin F. <RFr...@t-online.de> wrote:
>> >> But how can I find out the maximum value of a signed type?
>> >> I have thought about that and found no really correct solution.
>> >
>> >There is no safe way to compute that because signed integer overflow
>> >invokes undefined behavior.
>>
>> But you don't need to generate signed integer overflow in the process
>> of computing it. It can be done exclusively with assignments, which
>> are safe in C89.
>
>How do you generate the numbers to be assigned?
Using unsigned arithmetic on the widest unsigned standard type.
>How are these numbers
>converted if they are not in the range representable by the target?
In an implementation-defined manner.
>Is this different in C99?
C99 apparently allows the conversion to raise a signal, if the value is
out of range. This is what makes the approach theoretically unsafe in
C99.
...
>> All defined in limits.h
>
> Not quite. What, for example, is the maximum value of
>a `time_t'? Of a `clock_t'?
Tricky, since these can have floating point types.
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------