5

I have a weird problem in c# on visual studio 2013 (.NET framework 4.0):

When I compile an run the following code from the main thread:

//==========
double ddd = 300000.0;
ddd *= ddd;

Console.Write("The result is: " + ddd);
//==========

The result I get is: "The result is: 89999998976"

When I run the same code from a different thread in the same executable I get:

"The result is: 90000000000"

It looks like the double in the main thread is treated as float (32bits precision instead of 64bit precision).

Does anyone have any idea why it happens?


EDIT:

I found the answer. I'm using Managed DirectX. Apparently, you need to specify to directX device creation not to mess with the FPU precision (CreateFlags.FpuPreserve) otherwise doubles will be treated as floats.

For more info: floating point precision

Community
  • 1
  • 1
IlanGrin
  • 51
  • 3
  • 4
    Can you post your whole code please – tom redfern Apr 06 '17 at 13:04
  • 9
    Ideally a [mcve]... – Jon Skeet Apr 06 '17 at 13:04
  • 1
    Tried something similar and get 90000000000 for both main and thread. Is this what your code looks like? http://rextester.com/KGBBO46942 (using 4.5) – grimdog_john Apr 06 '17 at 13:15
  • No repro with the latest LinqPad or VS 2015. How did you try this? Debug or Release build? Under the debugger or not? The debugger *can* interfere even with Release builds. – Panagiotis Kanavos Apr 06 '17 at 13:26
  • @JonSkeet similar to this http://stackoverflow.com/questions/43254645/how-does-debugging-change-the-workflow-of-the-program which references this answer by ... guess http://stackoverflow.com/questions/43254645/how-does-debugging-change-the-workflow-of-the-program?noredirect=1#comment73578552_43254645 . The linked problem doesn't occur in VS2017 – Panagiotis Kanavos Apr 06 '17 at 13:29
  • This is not the result of `double` treated as `float`; `float` doesn't have enough precision to represent 89999998976 exactly. A more likely explanation is that you're hitting an execution path where the extended 80-bit precision of the x87 floating point isn't being used, because some code futzed around with the floating-point flags, for example. See [this question](http://stackoverflow.com/questions/6683059/is-floating-point-math-consistent-in-c-can-it-be), among others. – Jeroen Mostert Apr 06 '17 at 13:30
  • @JeroenMostert: No, the closest `float` to 90000000000 is *exactly* 89999998976. So it seems a reasonable thought... – Jon Skeet Apr 06 '17 at 14:22
  • @JonSkeet: My bad. Curse C# and its unnecessarily complicated ways of obtaining/verifying exact values. :-P – Jeroen Mostert Apr 06 '17 at 15:16

0 Answers0