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 / C++ / MFC
  4. Solved: Floating point rounding question

Solved: Floating point rounding question

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsgame-devhelptutorial
7 Posts 4 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.
  • L Offline
    L Offline
    Leif Goodwin
    wrote on last edited by
    #1

    NB: As indicated in a reply the issue has been solved. The DirectX module was changing the floating point precision. A colleague is calling the constructor of a class which takes two doubles as arguments. Each argument he passes is the sum of two doubles: CGridPoint( a + b, c + d) The class object stores the two values internally. When he views the result he sees that the values have been rounded to 1 decimal place. The values are not so large that this would be expected. Here are example: a = 44.445472717 b = 724631.800000000 a + b = 724676.250000000 In other words he passes in 44.445472717 + 724631.800000000 as the first argument to the constructor and the result is stored internally and is seen to be 724676.250000000. Does anyone know the cause of this rounding? BTW he uses VS2008. This works in VS2010.

    L B P 3 Replies Last reply
    0
    • L Leif Goodwin

      NB: As indicated in a reply the issue has been solved. The DirectX module was changing the floating point precision. A colleague is calling the constructor of a class which takes two doubles as arguments. Each argument he passes is the sum of two doubles: CGridPoint( a + b, c + d) The class object stores the two values internally. When he views the result he sees that the values have been rounded to 1 decimal place. The values are not so large that this would be expected. Here are example: a = 44.445472717 b = 724631.800000000 a + b = 724676.250000000 In other words he passes in 44.445472717 + 724631.800000000 as the first argument to the constructor and the result is stored internally and is seen to be 724676.250000000. Does anyone know the cause of this rounding? BTW he uses VS2008. This works in VS2010.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What is the code he is using to store the values and also to display them?

      Unrequited desire is character building. OriginalGriff

      L 1 Reply Last reply
      0
      • L Lost User

        What is the code he is using to store the values and also to display them?

        Unrequited desire is character building. OriginalGriff

        L Offline
        L Offline
        Leif Goodwin
        wrote on last edited by
        #3

        He is examing the property of the CGridPoint class using the debugger. He also confirms that the results look wrong when used. He displays a 2D representation of a Geophysical survey vessel and streamers on his screen, and position error ellipses are wrong, which is how he found this issue. The code is C++ of course. He has rewritten like this: CGridPoint gpt2 = gpt[i].Rotate(m_gptEllipseAngle); gpt2.m_fEasting += gptLocation.m_fEasting; gpt2.m_fNorthing += gptLocation.m_fNorthing; pts[i] = ViewPoint(gpt2); And it still fails. The original would have been something like: CGridPoint gpt2 = gpt[i].Rotate(m_gptEllipseAngle); pts[i] = ViewPoint(CGridPoint(gpt2.fEasting + gptLocation.m_fEasting, gpt2.fNorthing + gptLocation.m_fNorthing )); The above might have a typo or two, not sure, I rewrote it here without compiling it.

        L 1 Reply Last reply
        0
        • L Leif Goodwin

          He is examing the property of the CGridPoint class using the debugger. He also confirms that the results look wrong when used. He displays a 2D representation of a Geophysical survey vessel and streamers on his screen, and position error ellipses are wrong, which is how he found this issue. The code is C++ of course. He has rewritten like this: CGridPoint gpt2 = gpt[i].Rotate(m_gptEllipseAngle); gpt2.m_fEasting += gptLocation.m_fEasting; gpt2.m_fNorthing += gptLocation.m_fNorthing; pts[i] = ViewPoint(gpt2); And it still fails. The original would have been something like: CGridPoint gpt2 = gpt[i].Rotate(m_gptEllipseAngle); pts[i] = ViewPoint(CGridPoint(gpt2.fEasting + gptLocation.m_fEasting, gpt2.fNorthing + gptLocation.m_fNorthing )); The above might have a typo or two, not sure, I rewrote it here without compiling it.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I can't see the CGridPoint class in ATL or MFC; is this a Microsoft or home produced class?

          Unrequited desire is character building. OriginalGriff

          L 1 Reply Last reply
          0
          • L Leif Goodwin

            NB: As indicated in a reply the issue has been solved. The DirectX module was changing the floating point precision. A colleague is calling the constructor of a class which takes two doubles as arguments. Each argument he passes is the sum of two doubles: CGridPoint( a + b, c + d) The class object stores the two values internally. When he views the result he sees that the values have been rounded to 1 decimal place. The values are not so large that this would be expected. Here are example: a = 44.445472717 b = 724631.800000000 a + b = 724676.250000000 In other words he passes in 44.445472717 + 724631.800000000 as the first argument to the constructor and the result is stored internally and is seen to be 724676.250000000. Does anyone know the cause of this rounding? BTW he uses VS2008. This works in VS2010.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            I think we need to see CGridPoint. I suspect that it is explicitly rounding things to 2dp (or could even be quarter-points from the particular example you gave).

            1 Reply Last reply
            0
            • L Leif Goodwin

              NB: As indicated in a reply the issue has been solved. The DirectX module was changing the floating point precision. A colleague is calling the constructor of a class which takes two doubles as arguments. Each argument he passes is the sum of two doubles: CGridPoint( a + b, c + d) The class object stores the two values internally. When he views the result he sees that the values have been rounded to 1 decimal place. The values are not so large that this would be expected. Here are example: a = 44.445472717 b = 724631.800000000 a + b = 724676.250000000 In other words he passes in 44.445472717 + 724631.800000000 as the first argument to the constructor and the result is stored internally and is seen to be 724676.250000000. Does anyone know the cause of this rounding? BTW he uses VS2008. This works in VS2010.

              P Offline
              P Offline
              Peter_in_2780
              wrote on last edited by
              #6

              A quick look at the rounded result (7xxx.25) shows it to have about the precision we'd expect from float. I'd wager that there's an explicit or implicit conversion to float and back again hidden in there somewhere. Looks like you'll have to pick through it line by line. Peter

              Software rusts. Simon Stephenson, ca 1994.

              1 Reply Last reply
              0
              • L Lost User

                I can't see the CGridPoint class in ATL or MFC; is this a Microsoft or home produced class?

                Unrequited desire is character building. OriginalGriff

                L Offline
                L Offline
                Leif Goodwin
                wrote on last edited by
                #7

                He has found the cause. A call to DirectX was changing the floating point (double) precision ..

                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