?
Solved

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

Posted on 2003-11-04
2
Medium Priority
?
543 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
F4Codec earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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