System.Convert.ToDouble(decimal) giving two different answers

159 views Asked by At

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.

1

There are 1 answers

3
beerboy On BEST ANSWER

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