-
July 6th, 2012, 04:23 AM
#1
[RESOLVED] Double arithmetic seems bugged
Why does this go wrong?
- 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:
Code:
double ct = App.ClientClock.CurrentTime;
double newtime = ct - timeoffset;
And the same problem occurs.
The error difference changes depending on the value of CurrentTime, I've also seen differences of 10.0 and 8.0.
Solved. Answer is below.
Last edited by klapstoelpiloot; July 6th, 2012 at 08:23 AM.
-
July 6th, 2012, 04:35 AM
#2
Forum seems bugged also ;)
Can some moderator fix my picture please? It showed fine in the preview and now it doesn't. And I can't find an 'edit post' button to attempt to work around this.
-
July 6th, 2012, 04:36 AM
#3
Re: Double arithmetic seems bugged
The attachment didn't work for me.
Anyway if what you say is true you should be able to create a minimal example (just a main and some code that replaces your full code) that reproduces the issue.
-
July 6th, 2012, 04:37 AM
#4
Re: Double arithmetic seems bugged
For some reason the edit button doesn't appear before you made some posts (3 I think)
-
July 6th, 2012, 04:44 AM
#5
Re: Double arithmetic seems bugged
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.
-
July 6th, 2012, 08:20 AM
#6
Re: Double arithmetic seems bugged
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.
This article explains it fine:
http://chrisvall.com/coding/directx-...lizing-directx
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.
-
July 6th, 2012, 08:59 AM
#7
Re: Double arithmetic seems bugged
That root cause wasn't expected... amazing... You always learn something new everyday
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|