- Every variable here is a double, there are no ints or floats!
- App.ClientClock.CurrentTime is a double property which simply returns a double variable (it does not make any changes and there is no multithreading to make changes) so during these lines of code, this value is constant.
- I am aware of floating point inaccuracies due to approximation. This does not seem to be the issue (doubles are 64 bit floating points and a difference of 14.0 among these values is much larger than any rounding or approximation issue)
I have also tried to simplify the code by writing the following:
Ok here is my third post then.
EDIT: That worked, thanks, picture is added!
The funny thing is that I tried to make a small application that does such a simple calculation, but then I can't seem to reproduce it (the calculations are as expected). But the code is so simple that (without any multithreading that changes my variables) there is nothing that (should) get in between the low-level instructions here to make a difference...
Also, when I put the calculation in the watch window (see green arrow) it DOES do the calculation right... What the hell is .NET doing to my code?
Last edited by klapstoelpiloot; July 6th, 2012 at 04:59 AM.
After a whole day trying to figure this out I found that the cause is DirectX (I didn't mention that I am using DirectX, because it didn't seem relevant at first) and I think this is worth mentioning here.
DirectX switches on some secret CPU mode that treats all double precision arithmetic as single precision. Hence, my doubles start to behave like floats and this makes the use of double a waste of memory (and possibly performance) since you don't get the accuracy of a double. Turning this "feature" in DirectX off is possible, but at a performance cost.
I have added the most basic DirectX initialization to my test program (which couldn't reproduce this problem) and now it behaves in the same (incorrect) manner. So I think I will be looking for a solution to do my time calculations with floats.
EDIT: It stays a bit of a mystery though, why the Visual Studio Watch window CAN do the calculation properly (with double precision)...
Last edited by klapstoelpiloot; July 6th, 2012 at 08:22 AM.