[e-lang] Defensive consistency and integer overflow

Eric Northup digitale at digitaleric.net
Sun Sep 23 15:28:07 EDT 2007


Mark Miller wrote:
> On 9/23/07, David Hopwood <david.hopwood at industrial-designers.co.uk> wrote:
>   
>> In general, it would be much nicer to be working in a language with
>> arbitrary-precision integers
>>     
> [...]
> The irony is that defensive consistency doesn't need arbitrary
> precision integers. It would have been adequate for fixed precision
> integers to either throw an exception or became a NaN when exceeding
> its precision limit. Today, all instruction sets make available an
> overflow bit on integer ALU operations. AFAIK, all languages with
> fixed precision "integer" arithmetic ignore this bit and make it
> inaccessible.
>
> What's the integer situation on C#/.NET?
>   
They have good support for overflow checking.  At the IL level, all the 
fixed-precision integer arithmetic instructions have variants which 
check for overflow and raise OverflowException if detected.  Overflow 
checking can be turned on or off by default, but C# also has the 
keywords 'checked' and 'unchecked' which force it on or off for specific 
code blocks

    unchecked {
        int willOverflow = a * b * c;
        f(willOverflow); // note: code inside f does not inherit 'unchecked'
    }

or expressions

    uint alignMask = unchecked(uint.MaxValue * 16);

-Eric


More information about the e-lang mailing list