Link to home
Start Free TrialLog in
Avatar of dalchri
dalchri

asked on

DirectX 9 is messing up my double precision math in C#

Hello, I've got an awful problem that I hope isn't a bug from MS.  I'm using C# with the 1.0 framework and the directx 9 managed library.

I've only got the following references:
Microsoft.DirectX.Direct3D
System
System.Drawing
System.Windows.Forms

Here is some of the code in my form:

using Microsoft.DirectX.Direct3D;
using System;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form {
  private System.ComponentModel.Container components = null;

  public Form1() {
    InitializeComponent();

    PresentParameters presentParams =
      new PresentParameters();
    presentParams.Windowed = true;
    presentParams.SwapEffect = SwapEffect.Discard;
    presentParams.EnableAutoDepthStencil = true;
    presentParams.AutoDepthStencilFormat = DepthFormat.D16;

    double dbl1 = 1000000000.5;
    double dbl2 = 0.169444444444444;
    Console.WriteLine(
      dbl1 + " + " +
      dbl2 + " = " +
      (dbl1 + dbl2));

    Device device = new Device(0, DeviceType.Hardware,
      this, CreateFlags.SoftwareVertexProcessing,  
      presentParams);

    Console.WriteLine(
      dbl1 + " + " +
      dbl2 + " = " +
      (dbl1 + dbl2));
  }

Here is the output:
1000000000.5 + 0.169444444444444 = 1000000000.66944
1000000000.5 + 0.169444444444444 = 1000000000

It appears that simply creating a directx device destroys the precision of my math.  The values of the variables don't change, statement that prints out the results before and after device creation is the same, but the addition operation is useless now.

Did I set up my device wrong?  How in the world did DirectX get so much power over the environment?

Does anyone have any ideas on how to work around this problem????

Thank you for any suggestions!
ASKER CERTIFIED SOLUTION
Avatar of F4Codec
F4Codec

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dalchri
dalchri

ASKER

For future reference to anyone else who has the problem, this is the line that needs changed:

    Device device = new Device(0, DeviceType.Hardware,
      this, CreateFlags.SoftwareVertexProcessing |
      CreateFlags.FpuPreserve,  
      presentParams);


Thanks Julian!