why implicitly allowing compare ubyte and byte sucks

Rainer Deyke rainerd at eldwood.com
Thu Jun 11 19:53:36 PDT 2009


Don wrote:
> But then you still have the problem that the high half of the short was
> extended from the low half in two different ways, once by sign-extend,
> once by zero-extend. Mixing sign-extend and zero-extend in the same
> expression is asking for trouble.

I disagree.  In fact, I don't sign extension or conversion to a common
type should even be necessary.

Given value 's' of type 'sT' and unsigned value 'u' of type 'uT', where
'sT' and 'uT' have the same width, comparisons should be translated as
follows:
  's == u' --> 's >= 0 && cast(uT)(s) == u'
  's != u' --> 's < 0 || cast(uT)(s) != u'
  's < u' --> 's < 0 || cast(uT)(s) < u'
  's <= u' --> 's < 0 || cast(uT)(s) <= u'
  's > u' --> 's >= 0 && cast(uT)(s) > u'
  's >= u' --> 's > 0 && cast(uT)(s) >= u'

This system would always work, even when no type exists that can hold
all possible values of both 'sT' and 'uT'.  And it would always be
*correct*, i.e. negative values would always be smaller than and
different from positive values, even when the positive value is outside
the range of any signed type.

-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list