6

I'm loading data from a MySQL database into a C# .NET application. The data is held in the database as DBType.Double, but for use in my application I cast to Decimal using Convert.ToDecimal(). The data is positional data used in surveying and can be used to display a 3D model in a Direct3D window.

When the Direct3D window, and hence the Direct3D dll is not loaded, the conversion works fine, so that values like 1769301.6485186936, 5880300.8152837148 held in the database are loaded as 1769301.64851869, 5880300.81528371. However, if I have loaded the Direct3D module then the conversion results in the same values converting to 1769301.7112576, 5880300.79401984.

The basic code is as below, where vertex is a class/struct of 3 decimal values, X,Y and Z.

List<vertex> positions = new List<vertex>();

using (MySqlCommand cmd = new MySqlCommand("SELECT x, y, z FROM positionTable;", conn))
{

  MySqlDataReader dr = cmd.ExecuteReader();
  try
  {
    while (dr.Read())
    {
      vertex position = new vertex();
      position.X = Convert.ToDecimal(dr[0]);
      position.Y = Convert.ToDecimal(dr[1]);
      position.Z = Convert.ToDecimal(dr[2]);

      positions.Add(position);
    }
  }
}
lukiffer
  • 11,025
  • 8
  • 46
  • 70
jonew
  • 63
  • 3
  • I would guess that Direct3D is changing the `NumberFormatInfo` for the thread in question. See [NumberFormatInfo](http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx) for information on setting it. – Joshua Drake Apr 10 '12 at 21:17
  • @JoshuaDrake There is actually a change in true floating point values (by default), not just formatting, with D3D9. I suspect this is the issue. – Reed Copsey Apr 10 '12 at 21:18

1 Answers1

5

When you create your Direct3D Device, you to specify D3DCREATE_FPU_PRESERVE in D3DCREATE. Without this, DirectX (9) will switch the thread to use single precision floating point operations, which will have an effect on numerical computations even in your managed code.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373