Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DirectX is messing up my double precision math!

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
US
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!
 
For future reference if anyone runs into the same problem...

There is an obscure (to me) CreateFlag enum that is not well documented. You must create your device as follows:

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

I can understand why no one would probably ever care or notice this issue in a real game as precision math is not needed that much. I am using DirectX just to render still scenes in an astronomy program, not animations so I need the precision and not the performance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top