Board index » delphi » Combining signed and unsigned types - widened both operands

Combining signed and unsigned types - widened both operands

I hate this warning I'm not sure how to get rid of it. The situation is like
this:

var
    lw : LongWord;
    long1, long2 : LongInt;

(lw - long1) > long2

What should I do? This comes to mind:

(Int64(lw) - Int64(long1)) > long2

but since D5 help says:
"The generic integer types are Integer and Cardinal; use these whenever
possible, since they result in the best performance for the underlying CPU
and operating system."

is this faster (and neater):
lw > long1 + LongInt2
?

 

Re:Combining signed and unsigned types - widened both operands


Quote
Smola wrote in message ...
>I hate this warning I'm not sure how to get rid of it. The situation is
like
>this:

>var
>    lw : LongWord;
>    long1, long2 : LongInt;

>(lw - long1) > long2

You're mixing signed and unsigned integers of the same
size, you're _never_ going to fit their combined range
into any unit of that same size. In 32 bits, values in
(any of) those variables may be as low as -2^31 or as
high as 2^32-1. There is simply no way of representing
_all_ of that in 32 bits, you'd need at least 33 to
compute in.

It's usually a good idea to fix all warnings. In your
case, I'd probably do away with the cardinal. Make it
signed - you lose half the range, but if that's a problem
you should be using a different type anyway. If you're
worried that you're modelling an unsigned quantity with
a signed type, think that you now have space to represent
error conditions in.

Groetjes,
Maarten Wiltink

Re:Combining signed and unsigned types - widened both operands


Quote
"Maarten Wiltink" <maar...@kittensandcats.net> wrote in message

news:a8vib2$mr1$1@news1.xs4all.nl...

Quote
> Smola wrote in message ...
> >I hate this warning I'm not sure how to get rid of it. The situation is
> like
> >this:

> >var
> >    lw : LongWord;
> >    long1, long2 : LongInt;

> >(lw - long1) > long2

<snip>
> It's usually a good idea to fix all warnings. In your
> case, I'd probably do away with the cardinal. Make it
> signed - you lose half the range, but if that's a problem
> you should be using a different type anyway. If you're
> worried that you're modelling an unsigned quantity with
> a signed type, think that you now have space to represent
> error conditions in.

I did not actually defined the variables! The Longint and Cardinal values
come from two WinAPI functions. However, that  was not my question.

Re:Combining signed and unsigned types - widened both operands


Quote
Smola wrote in message ...
>"Maarten Wiltink" <maar...@kittensandcats.net> wrote in message
>news:a8vib2$mr1$1@news1.xs4all.nl...
>> Smola wrote in message ...
>> >I hate this warning I'm not sure how to get rid of it. The situation is
>> like
>> >this:

>> >var
>> >    lw : LongWord;
>> >    long1, long2 : LongInt;

>> >(lw - long1) > long2

><snip>
>> It's usually a good idea to fix all warnings. In your
>> case, I'd probably do away with the cardinal. Make it
>> signed - you lose half the range, but if that's a problem
>> you should be using a different type anyway. If you're
>> worried that you're modelling an unsigned quantity with
>> a signed type, think that you now have space to represent
>> error conditions in.

>I did not actually defined the variables! The Longint and Cardinal values
>come from two WinAPI functions. However, that  was not my question.

Oh. Now I'm confused. What was the question?

(I tend to use signed integers even when the function prototype
says unsigned. Same reasoning as above. Catch overflows and
there will be no problem.)

Groetjes,
Maarten Wiltink

Re:Combining signed and unsigned types - widened both operands


Quote
"Maarten Wiltink" <maar...@kittensandcats.net> wrote in message

news:a90us3$q9p$1@news1.xs4all.nl...

Quote
> Smola wrote in message ...
> >"Maarten Wiltink" <maar...@kittensandcats.net> wrote in message
> >news:a8vib2$mr1$1@news1.xs4all.nl...
> >> Smola wrote in message ...
> >> >I hate this warning I'm not sure how to get rid of it. The situation
is
> >> like
> >> >this:

> >> >var
> >> >    lw : LongWord;
> >> >    long1, long2 : LongInt;

> >> >(lw - long1) > long2

> ><snip>
> >> It's usually a good idea to fix all warnings. In your
> >> case, I'd probably do away with the cardinal. Make it
> >> signed - you lose half the range, but if that's a problem
> >> you should be using a different type anyway. If you're
> >> worried that you're modelling an unsigned quantity with
> >> a signed type, think that you now have space to represent
> >> error conditions in.

> >I did not actually defined the variables! The Longint and Cardinal values
> >come from two WinAPI functions. However, that  was not my question.

> Oh. Now I'm confused. What was the question?

Is this better:

(Int64(lw) - Int64(long1)) > long2

or this:

lw > long1 + LongInt2

Re:Combining signed and unsigned types - widened both operands


Quote
Smola wrote in message ...

[...]

Quote
>Is this better:

>(Int64(lw) - Int64(long1)) > long2

>or this:

>lw > long1 + LongInt2

Mathematically, probably the former. For reasons previously
expounded upon, I prefer the latter.

Groetjes,
Maarten Wiltink

Re:Combining signed and unsigned types - widened both operands


Quote
"Maarten Wiltink" <maar...@kittensandcats.net> wrote in message

news:a91g71$9i3$1@news1.xs4all.nl...

Quote
> Smola wrote in message ...
> [...]
> >Is this better:

> >(Int64(lw) - Int64(long1)) > long2

> >or this:

> >lw > long1 + LongInt2

> Mathematically, probably the former. For reasons previously
> expounded upon, I prefer the latter.

Hm, you're right, the sum of two longints may break across the boundary.

Re:Combining signed and unsigned types - widened both operands


Im Artikel <a8ut8a$k9...@sunce.iskon.hr>, "Smola" <supersmola...@VEinet.hr>
schreibt:

Quote
>What should I do? This comes to mind:

>(Int64(lw) - Int64(long1)) > long2

This roughly is what Delphi does automatically: convert all expressions to
Int64, including Int64(long2), and issue a warning.

You can prevent these conversions, and the according warning, when you cast all
the variables to the same type. In your case, when a signed comparison is
required, you can write:
  LongInt(lw) - long1 > long2

When lw can be huge, i.e. > MaxInt, then LongInt(lw) will result in a negative
value, with possibly strange results from the comparison. If this can happen,
then you should cast all variables to Int64, to prevent the warning.

DoDi

Other Threads