[Issue 5232] [patch] std.conv.to & std.conv.roundTo report invalid overflows for very large numbers

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 16 17:25:40 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5232



--- Comment #2 from Rob Jacques <sandford at jhu.edu> 2011-03-16 17:22:28 PDT ---
Opps. Forgot that 1.0L is a real, not a long. Also, that ConvOverflowError
changed to ConvOverflowException.

/**
Narrowing numeric-numeric conversions throw when the value does not
fit in the narrower type.
 */
T toImpl(T, S)(S value)
if (!implicitlyConverts!(S, T)
        && (isNumeric!S || isSomeChar!S)
        && (isNumeric!T || isSomeChar!T))
{
    enum sSmallest = mostNegative!(S);
    enum tSmallest = mostNegative!(T);
    static if (sSmallest < 0) {
        // possible underflow converting from a signed
        static if (tSmallest == 0) {
            immutable good = value >= 0;
        } else {
            static assert(tSmallest < 0);
            immutable good = value >= tSmallest;
        }
        if (!good) ConvOverflowException.raise("Conversion negative overflow");
    }
    static if (S.max > T.max) {
        // possible overflow
        if (value >= T.max+1.0L)
            ConvOverflowException.raise("Conversion overflow");
    }
    return cast(T) value;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list