[RESOLVED] Double arithmetic seems bugged
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Double arithmetic seems bugged

  1. #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 09:23 AM.

  2. #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.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    Re: Double arithmetic seems bugged

    For some reason the edit button doesn't appear before you made some posts (3 I think)
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #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 05:59 AM.

  6. #6

    Resolved 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 09:22 AM.

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    Re: Double arithmetic seems bugged

    That root cause wasn't expected... amazing... You always learn something new everyday
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center