core.checkedint

Dominikus Dittes Scherkl via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 24 10:32:37 PDT 2016


On Friday, 24 June 2016 at 17:25:32 UTC, Dominikus Dittes Scherkl 
wrote:
> I have a save exponentiation function (+ two helper functions 
> that may also be useful on their own):

Ah, and I use a better abs() function:

/// get the absolute value of x as unsigned type. always 
succeeds, even for T.min
Unsigned!T abs(T)(const(T) x) pure @safe @nogc nothrow 
if(isIntegral!T)
{
    static if(isSigned!T) if(x < 0) return -x;
    return x;
}

unittest
{
    byte a = -128;
    auto b = abs(a);
    assert(is(typeof(b) == ubyte));
    assert(b == 128);
}



More information about the Digitalmars-d mailing list