BUG: Rounding problem when a column expression is computed (326601)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft ADO.Net 2.0

This article was previously published under Q326601

SYMPTOMS

When you evaluate the expression that is specified in a computed column of a DataRow, incorrect results may be returned if the expression contains integer division. The resulting expression is rounded inconsistently.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

The following code demonstrates the problem:
using System;
using System.Data;

class Test
{
    public static void Main()
    {
        try
        {
            // Create a table with 3 columns.
            DataTable dt = new DataTable();
            dt.Columns.Add("Col1", typeof(int));
            dt.Columns.Add("Col2", typeof(int));
            DataColumn dc = dt.Columns.Add("Col3", typeof(int));
            dc.Expression = "Col1 / Col2";

            // Add the first row.
            DataRow row = dt.NewRow();
            row.ItemArray = new object[] { 3, 2 };
            dt.Rows.Add(row);
            Console.WriteLine(row[1]);

            // Add the second row.
            row = dt.NewRow();
            row.ItemArray = new object[] { 5, 2 };
            dt.Rows.Add(row);
            Console.WriteLine(row[2]);
        }
        catch(Exception exc)
        {
            Console.WriteLine(exc);
        }
    }
}
				
The output appears as follows:
2
2
				
The data types in the computed column imply integer division. However, the expression "3 / 2" evaluates to "2" for the first row instead of "1". The expression "5 / 2" in the second row returns the correct results.

According to the rules of integer division, you expect output to appear as follows:
1
2
				

Modification Type:MajorLast Reviewed:3/13/2006
Keywords:kbbug kbpending KB326601 kbAudDeveloper