Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert.ToDecimal giving different results when DirectX/Direct3D loaded

Tags:

c#

mysql

direct3d

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);
    }
  }
}
like image 294
jonew Avatar asked Apr 10 '12 21:04

jonew


1 Answers

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.

like image 160
Reed Copsey Avatar answered Sep 30 '22 22:09

Reed Copsey