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. Problems with .NET double data type

Problems with .NET double data type

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++tutorial
6 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.
  • S Offline
    S Offline
    sirtimid
    wrote on last edited by
    #1

    I am developing an MFC application on the .NET compiler. When I use atof function to convert a string to double value the compiler changes the decimal part of the result. When I try to add two doubles the compiler does the same. Below there is a simple example that I tried with the results I got: example 1: char str[11]; memset(str,0x00,11); memcpy(str,"1234567.89",10); double a = atof(str); Output : a = 1234567.8899999999 -------------------------------- example 2 double a=1234567; double b=0.89; double d = a + b; Output : a = 1234567.8899999999 Does anyone know why does this happen and what can I do to make it work properly? Thanks in Advance sirtimid

    T 1 Reply Last reply
    0
    • S sirtimid

      I am developing an MFC application on the .NET compiler. When I use atof function to convert a string to double value the compiler changes the decimal part of the result. When I try to add two doubles the compiler does the same. Below there is a simple example that I tried with the results I got: example 1: char str[11]; memset(str,0x00,11); memcpy(str,"1234567.89",10); double a = atof(str); Output : a = 1234567.8899999999 -------------------------------- example 2 double a=1234567; double b=0.89; double d = a + b; Output : a = 1234567.8899999999 Does anyone know why does this happen and what can I do to make it work properly? Thanks in Advance sirtimid

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      this is not a double problem ; it is a rounding one, known by the iso definition.


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      S 1 Reply Last reply
      0
      • T toxcct

        this is not a double problem ; it is a rounding one, known by the iso definition.


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        S Offline
        S Offline
        sirtimid
        wrote on last edited by
        #3

        And how will I fix it?

        T M 2 Replies Last reply
        0
        • S sirtimid

          And how will I fix it?

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          how do you display it ?


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          S 1 Reply Last reply
          0
          • S sirtimid

            And how will I fix it?

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            there's not much you can do about it. there's a book/paper about that : "What Every Computer Scientist Should Know About Floating Point Arithmetic" by David Goldberg http://docs.sun.com/source/806-3568/ncg\_goldberg.html


            Maximilien Lincourt Your Head A Splode - Strong Bad

            1 Reply Last reply
            0
            • T toxcct

              how do you display it ?


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              S Offline
              S Offline
              sirtimid
              wrote on last edited by
              #6

              I don't display it. I see it by the debugger.

              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