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
Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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