Solved

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

Posted on 2003-11-04
2
533 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

21 Experts available now in Live!

Get 1:1 Help Now