www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - uint is NOT just a positive number

reply Kagamin <spam here.lot> writes:
Some people used to use unsigned integers as 'mere' non-negative numbers, but
they're actually not numbers from range, they're numbers from entirely
different algebra. And - yes - this causes subtle bugs. Recent introduction of
integer range promotions works hard in hiding those bugs, so they manifest only
in very rare corner cases.

Ever wondered wheter a-=b and a+=-b equivalent? In most cases they are, except
for one. Do you remember? uint is not propagated to long. I've hit it recently.
It's exactly because unsigneds are not just non-negative numbers. So it's pain
to see their wide use as numbers for which sign-sensitive arithmetic operations
are meaningful.
Oct 07 2009
parent reply Don <nospam nospam.com> writes:
Kagamin wrote:
 Some people used to use unsigned integers as 'mere' non-negative numbers, but
they're actually not numbers from range, they're numbers from entirely
different algebra. And - yes - this causes subtle bugs. 
Amen! I actually think it's worse in D, because 'uint' is so easy to type, it's far more seductive than 'unsigned int'. Recent introduction of integer range promotions works hard in hiding those bugs, so they manifest only in very rare corner cases. It's not complete. It doesn't apply to arithmetic operations yet. Once that's in place, D could become really harsh about mixing signed and unsigned: if there's any chance the highest bit is set in the signed type, mixing signed and unsigned should be illegal.
 Ever wondered wheter a-=b and a+=-b equivalent? In most cases they are, except
for one. Do you remember? uint is not propagated to long. I've hit it recently.
It's exactly because unsigneds are not just non-negative numbers. So it's pain
to see their wide use as numbers for which sign-sensitive arithmetic operations
are meaningful.
Oct 07 2009
parent Jeremie Pelletier <jeremiep gmail.com> writes:
Don wrote:
 Kagamin wrote:
 Some people used to use unsigned integers as 'mere' non-negative 
 numbers, but they're actually not numbers from range, they're numbers 
 from entirely different algebra. And - yes - this causes subtle bugs. 
Amen! I actually think it's worse in D, because 'uint' is so easy to type, it's far more seductive than 'unsigned int'. Recent introduction of integer range promotions works hard in hiding those bugs, so they manifest only in very rare corner cases. It's not complete. It doesn't apply to arithmetic operations yet. Once that's in place, D could become really harsh about mixing signed and unsigned: if there's any chance the highest bit is set in the signed type, mixing signed and unsigned should be illegal.
 Ever wondered wheter a-=b and a+=-b equivalent? In most cases they 
 are, except for one. Do you remember? uint is not propagated to long. 
 I've hit it recently. It's exactly because unsigneds are not just 
 non-negative numbers. So it's pain to see their wide use as numbers 
 for which sign-sensitive arithmetic operations are meaningful.
I think it also depend on the representation of that number, for example most numbers I would feel more natural as hex value such bit fields and flags, address offsets, and whatnot are all unsigned, almost everything else is signed. I did get annoyed about uint not automatically propagated to long however, when I was reading lo/hi uint offset pairs from a file and making it a long offset (lo + (hi<<32)), turns out it can't be done without explicit long casts.
Oct 07 2009