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 question

atof question

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studioquestion
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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    char temp[] = "1.15"; double dbl = atof(temp); If I run this code in Visual Studio 6 dbl = 1.15 If I run this code in Visual Studio 2003 dbl = 1.1499999999999999 Can anyone explain this for me please

    A R 2 Replies Last reply
    0
    • L Lost User

      char temp[] = "1.15"; double dbl = atof(temp); If I run this code in Visual Studio 6 dbl = 1.15 If I run this code in Visual Studio 2003 dbl = 1.1499999999999999 Can anyone explain this for me please

      A Offline
      A Offline
      Archer282
      wrote on last edited by
      #2

      i dont know what that would happen. but you could alwase round it up (i think there is a round function that would suit if not you could always make your own)

      1 Reply Last reply
      0
      • L Lost User

        char temp[] = "1.15"; double dbl = atof(temp); If I run this code in Visual Studio 6 dbl = 1.15 If I run this code in Visual Studio 2003 dbl = 1.1499999999999999 Can anyone explain this for me please

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        Exactly what is telling you those values ? I am willing to bet that if you look at the 8-byte binary value they will be identical. It is the output display that is the only thing that differs. If you use a format specifier of "%.2f" with one of the printf family of functions (print, sprintf, or fprintf) I expect that you will see 1.15. It is important to note that it is virtually impossible to acquire exactly accurate floating point values. There are nearly always rounding errors involved that you must just deal with. __________________________________________ a two cent stamp short of going postal.

        L 1 Reply Last reply
        0
        • R Rick York

          Exactly what is telling you those values ? I am willing to bet that if you look at the 8-byte binary value they will be identical. It is the output display that is the only thing that differs. If you use a format specifier of "%.2f" with one of the printf family of functions (print, sprintf, or fprintf) I expect that you will see 1.15. It is important to note that it is virtually impossible to acquire exactly accurate floating point values. There are nearly always rounding errors involved that you must just deal with. __________________________________________ a two cent stamp short of going postal.

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

          Thanks for your reply I was getting the values from the quick-watch window displayed by the debugger. The 8-byte binary value displayed in the memory window IS the same. I have found that the real cause of my bug is the use of the pow() function from math.h. I had code like long rounding = 4; double result = pow(10, -rounding); which in vs6 returns result = 0.0001 and in vs2003 returns result = 0.0 these values ARE different in memory Looking at math.h I think the code above was calling long pow(long, long) resulting in 0 and auto casting the result to a double. I changed it to double result = pow(10.0, -rounding); and now it works. Why its different in vs6 and vs2003 I dont know

          S 1 Reply Last reply
          0
          • L Lost User

            Thanks for your reply I was getting the values from the quick-watch window displayed by the debugger. The 8-byte binary value displayed in the memory window IS the same. I have found that the real cause of my bug is the use of the pow() function from math.h. I had code like long rounding = 4; double result = pow(10, -rounding); which in vs6 returns result = 0.0001 and in vs2003 returns result = 0.0 these values ARE different in memory Looking at math.h I think the code above was calling long pow(long, long) resulting in 0 and auto casting the result to a double. I changed it to double result = pow(10.0, -rounding); and now it works. Why its different in vs6 and vs2003 I dont know

            S Offline
            S Offline
            ssiegel
            wrote on last edited by
            #5

            As previously mentioned you should not expect an exact comparision when working with floating point numbers. To check equality you need to compare the differnce to epsilon. If the difference is < or = epsilon then the numbers are equal. Otherwise they are not. This is done as follows: take the absolute value of the difference of the absolute values of the two floating point numbers and compare the result to epsilon. If the difference is less than or equal epsilon then the numbers are equal. if( FLT_EPSILON =< fabsf( fabsf(f1) - fabsf(f2) ) ) printf( "numbers are equal\n" ); else printf( "numbers are not equal\n" ); NOTE: there are double precision variants of epsilon and fabsf. Sam Sam

            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