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. double and float problem

double and float problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++delphihelpquestion
5 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.
  • N Offline
    N Offline
    NMiceli
    wrote on last edited by
    #1

    Hi. my problem: double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98 and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.

    T A D 3 Replies Last reply
    0
    • N NMiceli

      Hi. my problem: double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98 and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      There is a very good chance that Delphi and VB are getting the exact same result but due to how they are displaying the number you as seeing a different result. Floating point math is not exact. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      1 Reply Last reply
      0
      • N NMiceli

        Hi. my problem: double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98 and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.

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

        double c=98/100; would give c = 0.0 as 98/100 is integer arithmetic, it is then case to double! ;) OK, double c=(a/b) + 0.0000000001; will give c = 0.98 NOTE: This work around only makes the answer correct to 10dps. If you change everything to float you will probably get a different answer. These calculations all depend on the format of the floating point number and its precision. Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fuity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Williams (Little Britain)

        1 Reply Last reply
        0
        • N NMiceli

          Hi. my problem: double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98 and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          How a double (or float) is stored in memory and how it is displayed are two different things. http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html http://www.fact-index.com/f/fl/floating\_point\_1.html (see Problems with floating-point) http://www.hal-pc.org/~clyndes/computer-arithmetic/floats.html


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

          N 1 Reply Last reply
          0
          • D David Crow

            How a double (or float) is stored in memory and how it is displayed are two different things. http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html http://www.fact-index.com/f/fl/floating\_point\_1.html (see Problems with floating-point) http://www.hal-pc.org/~clyndes/computer-arithmetic/floats.html


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            N Offline
            N Offline
            NMiceli
            wrote on last edited by
            #5

            lot of interesting readings Then i found it! COleCurrency is all i need Thanks to all

            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