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. unexpected behaviour of 'atof' and 'strtod'

unexpected behaviour of 'atof' and 'strtod'

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 6 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.
  • U Offline
    U Offline
    User 3608241
    wrote on last edited by
    #1

    I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62

    D M S O 4 Replies Last reply
    0
    • U User 3608241

      I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62

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

      Member 3611002 wrote:

      what do i do...

      Read here.

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      C 1 Reply Last reply
      0
      • D David Crow

        Member 3611002 wrote:

        what do i do...

        Read here.

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        C Offline
        C Offline
        cagespear
        wrote on last edited by
        #3

        Looks like it will take weeks to get through that doc [:)] Can you please explain in simple words what is the cause and resolution of this behavior if I may request so ?

        D 1 Reply Last reply
        0
        • C cagespear

          Looks like it will take weeks to get through that doc [:)] Can you please explain in simple words what is the cause and resolution of this behavior if I may request so ?

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

          cagespear wrote:

          Looks like it will take weeks to get through that doc

          So just search for another one. They're plentiful. Here for example.

          cagespear wrote:

          Can you please explain in simple words what is the cause...

          Computers are binary (i.e., base-2) machines, thus they cannot exactly represent floating-point numbers as you might think.

          cagespear wrote:

          ...and resolution of this behavior if I may request so ?

          Epsilon.

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • U User 3608241

            I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            From the C++ FAQ Lite: Why is floating point so inaccurate? Why doesn't this print 0.43?[^]

            --Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

            1 Reply Last reply
            0
            • U User 3608241

              I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62

              S Offline
              S Offline
              Saurabh Garg
              wrote on last edited by
              #6

              There are two problems with real numbers on computers. 1. Not every real number have an exact binary representation. For example 0.1 when represented in binary is 0.0001100110011001100110011001100110011001100110011001101 now when you convert it back to real number you will get 0.10000000000000001. Note you can try this calculation by hand and this inaccuracy has got nothing to do with how we represent real numbers in computer. 2. There are uncountably infinite real numbers but computers can only represent and manipulate finite numbers (Well at least in hardware). This means that not every real number can be expressed in computer. Hope this makes it clear. -Saurabh

              1 Reply Last reply
              0
              • U User 3608241

                I have a situation-- CString str = "37.62"; double d, d1; d = atof(str); d1 = strtod(str, NULL); The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62

                O Offline
                O Offline
                Ozer Karaagac
                wrote on last edited by
                #7

                Simply, this is related with binary representation of floating point numbers.

                Member 3611002 wrote:

                The results of both d and d1 are 37.619999999999997 (unexpected) what do i do to get d=37.62 and d1=37.62

                This is normal and there is nothing to do for a double type. if you format that number back using format string %.2f, you will get 37.62 again. I'll try to comment on, shortly. As you may know, double is a 64 bit floating point number. There are some special formats (standards defined by IEEE in this case) to store it in memory. It consists of 3 parts; sign (1), exponent (11), significand (52 bits). There are also special rules for storing exponent and significand. Most important one, exponent part is two's exponent, not ten's. Moreover, some operations (i.e. rounding and normalization) have been applied to a floating point number after every operation. After all, it is stored as nearest possible value fitting into these rules.

                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