symmetric signed types

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Jan 23 12:35:56 PST 2014


On 1/23/14 4:09 AM, Dominikus Dittes Scherkl wrote:
> There is one mistake in C that D proliverates:
>
> The T.min value of signed types.
>
> e.g.
>
> byte a = -128;
> auto b = -a;
>
> What type should b get? (of course "byte" but the value doesn't fit!)

The type will be int.

> Also getting the absolute value of some signed variable
> need to return a different type or doesn't work correct for all input.
> E.g. "ubyte abs(byte)" - this functions which can't even use a template,
> or has anybody a good idea ho to express "unsigned T abs(T)(T x)"?

http://dlang.org/phobos/std_conv.html#.unsigned

> So I thought I could design a new type "sbyte" with symmetric range
> (-127..127) and an additional value NaN (yes, the old 0x80).
> (and of course larger, similar types - by the way: why wasn't "short"
> instead called "word"? Then my new type would be "sword" :-)
>
> It worked all well until I found that the new operators !<> !<= etc
> can't be overloaded (they are only available for floating types)!
> Why is this so?

We're deprecating the new operators :o).

> D made all the floatingpoint stuff so much better than C, but the
> integral types still suffer the same old flaws.

There are quite a few improvements for integrals, too, most importantly 
of the kind that don't exact a speed penalty.


Andrei



More information about the Digitalmars-d mailing list