[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 09:43:47 PDT 2011


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



--- Comment #1 from Rob Jacques <sandford at jhu.edu> 2011-03-16 09:40:33 PDT ---
Minor patch update. Updated the template constraints to DMD 2.052. Changed
+1.0L to +1.0uL and shifted the formating to a 80-character maximum line
length. As before this patch is essentially a single line change from 

if (value > T.max)

to 

if (value >= T.max+1.0L)

/**
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.0uL)
            ConvOverflowError.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