2

I've got a very odd issue. I run this in the immediate window:

?(System.Convert.ToDouble(2458963.229671001113318401517D))

2458963.229671001

Which is what I expect!

Then, later on in my application, after initializing Managed DirectX, I get the following:

?(System.Convert.ToDouble(2458963.229671001113318401517D))

2458963.25

After I hit that point in my code, forever after System.Convert.ToDouble() will return truncated and wrong results.

I've isolated the single line that causes this change in System.Convert.ToDouble() behavior:

new Device(adapterOrdinal, dType, this, flags, m_presentParams); 

Which is a Microsoft.DirectX.Direct3D.Device object, from:

// Assembly: Microsoft.DirectX.Direct3D, Version=1.0.2902.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

I'm not sure why creating a Microsoft.DirectX.Direct3D.Device causes a change in how System.Convert.ToDouble() acts.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 2
    BTW, MDX was deprecated a looong time ago, before XNA (also abandoned by MS) –  Apr 29 '20 at 01:20
  • Use [SharpDX](http://sharpdx.org/) or [SlimDX](https://github.com/SlimDX/slimdx), not legacy Managed DirectX. See [this blog post](https://walbourn.github.io/directx-and-net/) – Chuck Walbourn Jul 15 '20 at 19:17

1 Answers1

5

As per https://learn.microsoft.com/en-us/previous-versions/bb324030(v%3Dvs.85):

When a Device object is created, the common language runtime will change the floating-point unit (FPU) to single precision to maintain better performance. To maintain the default double precision FPU, which is default for the common language runtime, use the CreateFlags.FpuPreserve flag when creating a Device object

beerboy
  • 1,304
  • 12
  • 12
  • Thank you, that looks like it. We've been calling this issue "the curse".... You have lifted it. – ManagedDirectXProblem Apr 29 '20 at 01:38
  • _I learnt something new today_. I haven't seem something this bad since the in-place replacement of .NET 4 with 4.5 –  Apr 29 '20 at 08:52
  • Note this floating-point CRT behavior of Direct3D is unique to legacy Direct3D 9 and prior. Back in the day, a lot of vertex processing was done on the CPU so it made a big difference. Today, it's mostly hardware processing or specialized CPU hardware. See [Microsoft Docs](https://learn.microsoft.com/en-us/windows/win32/dxtecharts/top-issues-for-windows-titles#manipulation-of-the-floating-point-control-word) – Chuck Walbourn Jul 15 '20 at 19:16