Convert.ToDecimal giving different results when DirectX/Direct3D loaded
Asked Answered
C

1

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);
    }
  }
}
Cussed answered 10/4, 2012 at 21:11 Comment(2)
I would guess that Direct3D is changing the NumberFormatInfo for the thread in question. See NumberFormatInfo for information on setting it.Jointress
@JoshuaDrake There is actually a change in true floating point values (by default), not just formatting, with D3D9. I suspect this is the issue.Luminance
L
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.

Luminance answered 10/4, 2012 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.