Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. precision problems with double type

precision problems with double type

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sammyh
    wrote on last edited by
    #1

    I'm having precision problems with the double type, the help files say I should get 15 digits of precision... BUT... double d1 = 12589230.0342; double d2 = -10.335927963256836; //good precision, but when added, problems occur double newd = d1+d2; // newd now equals 12589220.0 Where's all my precision???? I've tried decimal type, but is's incredibly slow.... I have to use it when multiplying, but addition of 10??? Any suggestions? should I use my own type? Possibly a binary coded decimal Thanks for any help. -Sam

    S 1 Reply Last reply
    0
    • S sammyh

      I'm having precision problems with the double type, the help files say I should get 15 digits of precision... BUT... double d1 = 12589230.0342; double d2 = -10.335927963256836; //good precision, but when added, problems occur double newd = d1+d2; // newd now equals 12589220.0 Where's all my precision???? I've tried decimal type, but is's incredibly slow.... I have to use it when multiplying, but addition of 10??? Any suggestions? should I use my own type? Possibly a binary coded decimal Thanks for any help. -Sam

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      The result I see in the watch window is 12589219.698272036, not 12589220.0. (the real result is 12589219.698272036743164). The decimal type can be used (28 digit precision) : decimal d1 = 12589230.0342M; decimal d2 = -10.335927963256836M; decimal d = d1 + d2; and I get the real result in the watch window.

      S 1 Reply Last reply
      0
      • S Stephane Rodriguez

        The result I see in the watch window is 12589219.698272036, not 12589220.0. (the real result is 12589219.698272036743164). The decimal type can be used (28 digit precision) : decimal d1 = 12589230.0342M; decimal d2 = -10.335927963256836M; decimal d = d1 + d2; and I get the real result in the watch window.

        S Offline
        S Offline
        sammyh
        wrote on last edited by
        #3

        YOU'RE right!!!!! Now that makes things even weirder.... My actual code.... public void Translate(double x, double y, double z) { foreach(DPoint p in points) { p.x += x; p.y += y; p.z += z; } } a Dpoint is just 3 doubles x,y,z on the first iteration of the foreach: x=12589230.0342 y = 693719.8863 z = 0.0 p.x = -10.335927963256836 p.y = 9.6102085113525391 p.z = 5.7499995231628418 after first iteration p.x = 12589220.0 p.y = 693729.5 p.z = 5.7499995231628418 I'm trying to replicate problem is a simple app now...

        S 1 Reply Last reply
        0
        • S sammyh

          YOU'RE right!!!!! Now that makes things even weirder.... My actual code.... public void Translate(double x, double y, double z) { foreach(DPoint p in points) { p.x += x; p.y += y; p.z += z; } } a Dpoint is just 3 doubles x,y,z on the first iteration of the foreach: x=12589230.0342 y = 693719.8863 z = 0.0 p.x = -10.335927963256836 p.y = 9.6102085113525391 p.z = 5.7499995231628418 after first iteration p.x = 12589220.0 p.y = 693729.5 p.z = 5.7499995231628418 I'm trying to replicate problem is a simple app now...

          S Offline
          S Offline
          sammyh
          wrote on last edited by
          #4

          This snippet: decimal px = -10.335927963256836M; decimal ix = 12589230.0342M; decimal nx= px + ix; double d = Convert.ToDouble(nx); double c = 2; Works as long as it happens before this line: device = new Device(graphicsSettings.AdapterOrdinal, graphicsSettings.DevType, windowed ? ourRenderTarget : this , createFlags, presentParams); If it happens the line after I get my problem.... now that is messed up, how can it change the behavior of a built in type????

          S 1 Reply Last reply
          0
          • S sammyh

            This snippet: decimal px = -10.335927963256836M; decimal ix = 12589230.0342M; decimal nx= px + ix; double d = Convert.ToDouble(nx); double c = 2; Works as long as it happens before this line: device = new Device(graphicsSettings.AdapterOrdinal, graphicsSettings.DevType, windowed ? ourRenderTarget : this , createFlags, presentParams); If it happens the line after I get my problem.... now that is messed up, how can it change the behavior of a built in type????

            S Offline
            S Offline
            sammyh
            wrote on last edited by
            #5

            OK, When you create a directx device it changes all operations to single precision. It can be overridden with the fpupreserve flag. Thanks for the help. -Sam

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups