1
Vote

BigRational(decimal) can cause OverflowExceptions for common values

description

I have overflow checking turned on in my build, and BigRational's decimal constructor causes an OverflowException when I pass it various representations of 5/7. I've attached a sample project that demonstrates this. The fix requires putting an explicit unchecked block around lines 257 and 258 of BigRational.cs.
 
Here's the main code from the attached sample project:
 
    static void Main(string[] args)
    {
        //Causes OverflowException at BigRational.cs line 257
        TestOverflow(5m / 7m);
 
        //Causes OverflowException at BigRational.cs line 258
        TestOverflow((decimal)(5.0 / 7.0));
    }
 
    private static void TestOverflow(decimal value)
    {
        try
        {
            BigRational rat = new BigRational(value);
            Console.WriteLine(rat);
        }
        catch (OverflowException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

file attachments

comments

davkean wrote Aug 30, 2010 at 11:28 PM

Thanks for the bug report - we'll look at it and get back to you.