June 06, 2005 Proposal: signed/unsigned integer comparisons | ||||
---|---|---|---|---|
| ||||
I think D should special case the builtin comparison operators instead of treating them as binary operators in terms of the integer promotion rules. This is really trivial to implement: if the lhs and rhs are integers and one is unsigned and the other is signed, either promote them to a signed integer that can hold the whole range of values or convert the expression internally to (using C's integer promotion rules): lhs is signed, rhs is unsigned ((lhs < 0) || (lhs < rhs)) for "lhs < rhs" ((lhs < 0) && (lhs OP rhs)) otherwise lhs is unsigned, rhs is signed ((rhs < 0) || (lhs > rhs)) for "lhs > rhs" ((rhs > 0) && (lhs OP rhs)) otherwise except force lhs and rhs to both be evaluated for side effects (and only once). With the C promotion rules, the expression to the right of the short-circuit operators are done using unsigned integers, so you can verify yourself that these are correct. The reason for the promotion rules in C is to reduce the complexity of handling lots of different operands and to unify the return type of the expression (since it's a truth value, special casing it this way for the operands is no problem). (reverse my username to contact me) |
March 06, 2007 Re: Proposal: signed/unsigned integer comparisons | ||||
---|---|---|---|---|
| ||||
Posted in reply to etep | To chime in, I think instead all silent information-losing casts should be done away with.
I'm a little new to D so I was dumbfounded to discover that 0u > -1 evaluates to false. Both sides should be promoted to a large enough type to hold each before doing the comparison.
Can anyone tell me if there is a plan to make such a change?
etep@kkow.net Wrote:
> I think D should special case the builtin comparison operators instead of treating them as binary operators in terms of the integer promotion rules. This is really trivial to implement: if the lhs and rhs are integers and one is unsigned and the other is signed, either promote them to a signed integer that can hold the whole range of values or convert the expression internally to (using C's integer promotion rules):
>
> lhs is signed, rhs is unsigned
> ((lhs < 0) || (lhs < rhs)) for "lhs < rhs"
> ((lhs < 0) && (lhs OP rhs)) otherwise
>
> lhs is unsigned, rhs is signed
> ((rhs < 0) || (lhs > rhs)) for "lhs > rhs"
> ((rhs > 0) && (lhs OP rhs)) otherwise
>
> except force lhs and rhs to both be evaluated for side effects (and only once).
>
> With the C promotion rules, the expression to the right of the short-circuit operators are done using unsigned integers, so you can verify yourself that these are correct. The reason for the promotion rules in C is to reduce the complexity of handling lots of different operands and to unify the return type of the expression (since it's a truth value, special casing it this way for the operands is no problem).
>
> (reverse my username to contact me)
|
Copyright © 1999-2021 by the D Language Foundation