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. atof() losing precision

atof() losing precision

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 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.
  • A Offline
    A Offline
    Andy H
    wrote on last edited by
    #1

    When numbers are larger than 15 significant figures the function atof() is losing precision. For example: double dVal = atof( pszValue ); // where pszValue holds the string "1111111111111111" dVal ends up with 1.11111111111111e+15 which equates to: 1111111111111110 Can anyone suggest a safer way to convert the char* to a double?

    C 1 Reply Last reply
    0
    • A Andy H

      When numbers are larger than 15 significant figures the function atof() is losing precision. For example: double dVal = atof( pszValue ); // where pszValue holds the string "1111111111111111" dVal ends up with 1.11111111111111e+15 which equates to: 1111111111111110 Can anyone suggest a safer way to convert the char* to a double?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      That's because atof returns a float (guess what the 'f' in the function name stands for ?). You could try by using stringstream from the STL:

      stringstream ssVal;
      double dValue;
      ssVal << pszValue;
      ssVal >> dValue;

      Don't forget to #include <sstream> and to put using namespace std; at the top of your cpp file. Forget what I say, I wasn't awake ;P . If you look at the range of a double, you'll see that the accuracy (here)[^] is 14 or 15 digits. Your string contains much more digits than that. Now, if the number that you want to represent is an integer value, why don't you use an __int64 instead ? But, I have a question: why do you need such an accuracy, why is it important ? In most of the cases, you won't need that accuracy.

      Cédric Moonen Software developer
      Charting control [v1.2]

      L 1 Reply Last reply
      0
      • C Cedric Moonen

        That's because atof returns a float (guess what the 'f' in the function name stands for ?). You could try by using stringstream from the STL:

        stringstream ssVal;
        double dValue;
        ssVal << pszValue;
        ssVal >> dValue;

        Don't forget to #include <sstream> and to put using namespace std; at the top of your cpp file. Forget what I say, I wasn't awake ;P . If you look at the range of a double, you'll see that the accuracy (here)[^] is 14 or 15 digits. Your string contains much more digits than that. Now, if the number that you want to represent is an integer value, why don't you use an __int64 instead ? But, I have a question: why do you need such an accuracy, why is it important ? In most of the cases, you won't need that accuracy.

        Cédric Moonen Software developer
        Charting control [v1.2]

        L Offline
        L Offline
        Llasus
        wrote on last edited by
        #3

        Cedric Moonen wrote:

        That's because atof returns a float

        yeah that's what I thought about too. Until I saw this in MSDN[^]. Why not name it atod in the first place? :confused:

        C 1 Reply Last reply
        0
        • L Llasus

          Cedric Moonen wrote:

          That's because atof returns a float

          yeah that's what I thought about too. Until I saw this in MSDN[^]. Why not name it atod in the first place? :confused:

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Yeah, I realized that just after I posted. I updated my answer already.

          Cédric Moonen Software developer
          Charting control [v1.2]

          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