[OT] The Usual Arithmetic Confusions
Mark
smarksc at gmail.com
Sat Feb 5 10:11:48 UTC 2022
On Friday, 4 February 2022 at 21:27:44 UTC, H. S. Teoh wrote:
> On Fri, Feb 04, 2022 at 08:50:35PM +0000, Mark via
> Digitalmars-d wrote:
>> On Friday, 4 February 2022 at 04:28:37 UTC, Walter Bright
>> wrote:
>> > There's really no fix for that other than making the effort
>> > to understand 2s-complement. Some noble attempts:
>> >
>> > Java: disallowed all unsigned types. Wound up having to add
>> > that back in as a hack.
>>
>> How many people actually use (and need) unsigned integers?
>
> I do. They are very useful in APIs where I expect only positive
> values. Marking the parameter type as uint makes it clear
> exactly what's expected, instead of using circumlocutions like
> taking int with an in-contract that x>=0. Also, when you're
> dealing with bitmasks, you WANT unsigned types. Using signed
> types for that will cause values to get munged by unwanted sign
> extensions, and in general just cause grief and needless
> complexity where an unsigned type would be completely
> straightforward.
>
> Also, for a systems programming language unsigned types are
> necessary, because they are a closer reflection of the reality
> at the hardware level.
>
>
>> If 99% of users don't need them, that's a good case for
>> relegating them to a library type. This wasn't possible in
>> Java because it doesn't support operator overloading, without
>> which dealing with such types would have been quite annoying.
>
> Needing a library type for manipulating bitmasks would make D
> an utter joke of a systems programming language.
>
>
> T
I should have phrased my question as "how many people outside
systems programming...", as this is what I had in mind (I mostly
write high-level code, though I don't know if I'm the typical D
user). But since D is proudly general-purpose I admit that this
question is moot.
Regarding positive values, AFAIK unsigned ints aren't suitable
for this because you still want to do ordinary arithmetic on
positive integers, not modular arithmetic. Runtime checks are
unavoidable because even mundane operations such as `--x` can
potentially escape the domain of positive integers.
Also, I don't think being a library type is a mark of shame.
Depending on the language, they can be just as useful and almost
as convenient as built-in types. C++'s std::byte was mentioned on
this thread - it's a library type.
More information about the Digitalmars-d
mailing list