Solved

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

Posted on 2003-11-04
2
535 Views
Last Modified: 2013-12-26
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!
0
Comment
Question by:dalchri
2 Comments
 

Accepted Solution

by:
F4Codec earned 50 total points
ID: 9740330
I don't know the equivalent managed interface, but in the C++ interface the CreateDevice takes an optional flag
D3DCREATE_FPU_PRESERVE
which preserves the floating point arithmetic state.

Julian.
0
 

Author Comment

by:dalchri
ID: 9740425
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now