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. VC++6 + VS SP6 = COMPUTATION BUG!!

VC++6 + VS SP6 = COMPUTATION BUG!!

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++visual-studioquestion
5 Posts 3 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.
  • F Offline
    F Offline
    Fred D
    wrote on last edited by
    #1

    Hi guys, PC : Intel PIII 700MHz with W2k SP6 I was using VC++ 6 with VisualStudio SP4 and the result of a calculation like this one.. double res = 25.5/1000; // would give you 0.025500000000, as you can expect.. Now, here’s something quite weird: I’ve just upgraded VS to SP6 and the same calculation now gives me.. double res = 25.5/1000; // 0.025499999999999998 ????? :doh: even worse if you write: double t2 = 5.f*0.001f; // 0.0050000002374872565 :wtf: And it seems to be like that every time you try to compute 2 doubles or a double and an int for ex. (multiplication or division !) as anyone noticed something similar? Wonder if there is any fix for that one. Tx Fred

    P A 2 Replies Last reply
    0
    • F Fred D

      Hi guys, PC : Intel PIII 700MHz with W2k SP6 I was using VC++ 6 with VisualStudio SP4 and the result of a calculation like this one.. double res = 25.5/1000; // would give you 0.025500000000, as you can expect.. Now, here’s something quite weird: I’ve just upgraded VS to SP6 and the same calculation now gives me.. double res = 25.5/1000; // 0.025499999999999998 ????? :doh: even worse if you write: double t2 = 5.f*0.001f; // 0.0050000002374872565 :wtf: And it seems to be like that every time you try to compute 2 doubles or a double and an int for ex. (multiplication or division !) as anyone noticed something similar? Wonder if there is any fix for that one. Tx Fred

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Why_Floating_Point_Numbers_May_Lose_Precision.asp[^]


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

      1 Reply Last reply
      0
      • F Fred D

        Hi guys, PC : Intel PIII 700MHz with W2k SP6 I was using VC++ 6 with VisualStudio SP4 and the result of a calculation like this one.. double res = 25.5/1000; // would give you 0.025500000000, as you can expect.. Now, here’s something quite weird: I’ve just upgraded VS to SP6 and the same calculation now gives me.. double res = 25.5/1000; // 0.025499999999999998 ????? :doh: even worse if you write: double t2 = 5.f*0.001f; // 0.0050000002374872565 :wtf: And it seems to be like that every time you try to compute 2 doubles or a double and an int for ex. (multiplication or division !) as anyone noticed something similar? Wonder if there is any fix for that one. Tx Fred

        A Offline
        A Offline
        Antony M Kancidrowski
        wrote on last edited by
        #3

        Fred D. wrote: Wonder if there is any fix for that one I doubt it. What you have stumbled across is inevitable with floating point arithmetic. You need to decide what precision you want and round up to that precision after your calculation. You can then show the floating point number to the precision required. e.g. double res = 25.5/1000 + 0.0005; for your example. Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fruity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Walliams (Little Britain)

        F 1 Reply Last reply
        0
        • A Antony M Kancidrowski

          Fred D. wrote: Wonder if there is any fix for that one I doubt it. What you have stumbled across is inevitable with floating point arithmetic. You need to decide what precision you want and round up to that precision after your calculation. You can then show the floating point number to the precision required. e.g. double res = 25.5/1000 + 0.0005; for your example. Ant. I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fruity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!
          - David Walliams (Little Britain)

          F Offline
          F Offline
          Fred D
          wrote on last edited by
          #4

          tx for your reply, in your example it would actually be like that: Double t = (25.5+0.0005)/1000; // but that still gives 0.025500499999999999 :doh: but actually, it seems it be a display issue in the debugger viewer.. 'cos even when the debugger shows a value d = 0.025500000001 where it should be 0.025500000000 when compare d with 0.025500000000, the result is true. and a further check at the @ of the variable in the memory window, shows a correct value. TK GOD! :-D Fred

          A 1 Reply Last reply
          0
          • F Fred D

            tx for your reply, in your example it would actually be like that: Double t = (25.5+0.0005)/1000; // but that still gives 0.025500499999999999 :doh: but actually, it seems it be a display issue in the debugger viewer.. 'cos even when the debugger shows a value d = 0.025500000001 where it should be 0.025500000000 when compare d with 0.025500000000, the result is true. and a further check at the @ of the variable in the memory window, shows a correct value. TK GOD! :-D Fred

            A Offline
            A Offline
            Antony M Kancidrowski
            wrote on last edited by
            #5

            Yes you still will not have the correct answer to all those decimal places. The idea is that the answer is correct to the precision that you choose. In this case 4 decimal places. You are quite correct in the matter of it being a display issue thereafter. Ant. I'm hard, yet soft.
            I'm coloured, yet clear.
            I'm fruity and sweet.
            I'm jelly, what am I? Muse on it further, I shall return!
            - David Walliams (Little Britain)

            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