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. Format double precision

Format double precision

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 8 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    I had follow code :

    double dbl = 24.000560700;
    CString sTemp,sDecimal;
    sTemp.Format(\_T("%0.16f"),dbl);
    SetWindowText(sTemp);
    

    and result is 24.0005607000000010 ... not 24.000560700, why ?

    M A C L C 6 Replies Last reply
    0
    • _ _Flaviu

      I had follow code :

      double dbl = 24.000560700;
      CString sTemp,sDecimal;
      sTemp.Format(\_T("%0.16f"),dbl);
      SetWindowText(sTemp);
      

      and result is 24.0005607000000010 ... not 24.000560700, why ?

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

      because the number cannot be precisely represented in binary, and when it gets printed out, the value will be displayed with that "error". See this : http://support.microsoft.com/kb/42980[^]

      Watched code never compiles.

      1 Reply Last reply
      0
      • _ _Flaviu

        I had follow code :

        double dbl = 24.000560700;
        CString sTemp,sDecimal;
        sTemp.Format(\_T("%0.16f"),dbl);
        SetWindowText(sTemp);
        

        and result is 24.0005607000000010 ... not 24.000560700, why ?

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        Why does this question keep coming up? ...please do a search before posting questions... I'm sure we've answered this about 10-20 times in a year's time frame...

        1 Reply Last reply
        0
        • _ _Flaviu

          I had follow code :

          double dbl = 24.000560700;
          CString sTemp,sDecimal;
          sTemp.Format(\_T("%0.16f"),dbl);
          SetWindowText(sTemp);
          

          and result is 24.0005607000000010 ... not 24.000560700, why ?

          C Offline
          C Offline
          Chuck OToole
          wrote on last edited by
          #4

          Because you asked for 16 digits, it gave you 16 digits. The "imprecision" is in producing the OUTPUT, the conversion of the binary / computer representation of the number into the string of characters that you display. This is true regardless of whether it is you printing the value or the debugger displaying it for you. Both processes need to take the binary value and convert it to a string of characters for your eyes. Computer binary representations (Base 2) and printed represetnations (in Base 10) are inherently incompatible and can only be approximated. You control the approximation with the format specifier for how many digits you want to see. You have also made a basic assumption that is also flawed. You assume this program statement produces a "precise" value:

          double dbl = 24.000560700;

          when in fact, the compiler has to take this character representation of a number, convert it from base 10 to the binary representation of a floating point number, base 2, and put it into the object file to be included in the program. Because of the what I said above, conversion from text to binary form (compilation) or from binary form to text (printf) each provide some level of "imprecision" or "approximation" that varies in the least significant digits, as you've discovered.

          1 Reply Last reply
          0
          • _ _Flaviu

            I had follow code :

            double dbl = 24.000560700;
            CString sTemp,sDecimal;
            sTemp.Format(\_T("%0.16f"),dbl);
            SetWindowText(sTemp);
            

            and result is 24.0005607000000010 ... not 24.000560700, why ?

            L Offline
            L Offline
            Luke__Chen
            wrote on last edited by
            #5

            "%0.16f" means there will be 16 digits after the "." for result. "%0.9f" is what you need!

            1 Reply Last reply
            0
            • _ _Flaviu

              I had follow code :

              double dbl = 24.000560700;
              CString sTemp,sDecimal;
              sTemp.Format(\_T("%0.16f"),dbl);
              SetWindowText(sTemp);
              

              and result is 24.0005607000000010 ... not 24.000560700, why ?

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Because the double data type is a convenient approximate representation (see here, it is quite formative)[^]) of real numbers. Computers memory is finite, it cannot map infinite sets like real numbers (or even natural numbers).

              Veni, vidi, vici.

              1 Reply Last reply
              0
              • _ _Flaviu

                I had follow code :

                double dbl = 24.000560700;
                CString sTemp,sDecimal;
                sTemp.Format(\_T("%0.16f"),dbl);
                SetWindowText(sTemp);
                

                and result is 24.0005607000000010 ... not 24.000560700, why ?

                R Offline
                R Offline
                rana ray
                wrote on last edited by
                #7

                Hi, try doing sTemp.Format(_T("%0.08f"),dbl); regards, vatsa www.objectiveprogramming.com

                _ 1 Reply Last reply
                0
                • R rana ray

                  Hi, try doing sTemp.Format(_T("%0.08f"),dbl); regards, vatsa www.objectiveprogramming.com

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #8

                  Yes, but I don't know always how many decimal exist ...

                  C L 2 Replies Last reply
                  0
                  • _ _Flaviu

                    Yes, but I don't know always how many decimal exist ...

                    C Offline
                    C Offline
                    Chuck OToole
                    wrote on last edited by
                    #9

                    You might as well ask "How many angels can dance on the head of a pin?". You really need to read the article already linked http://support.microsoft.com/kb/42980[^] and embrace the idea that a "Binary Computer" cannot precisely represent every possible "Decimal Digit" that exists for floating point numbers. It is a fact of life. Rather than try to figure out how many "decimals exist", you just need to determine "how many you care about". 6, 12, 18 are a pretty common need, unless you are doing planetary orbital calculations or high energy physics.

                    1 Reply Last reply
                    0
                    • _ _Flaviu

                      Yes, but I don't know always how many decimal exist ...

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

                      Flaviu2 wrote:

                      Yes, but I don't know always how many decimal exist ...

                      This is how many decimals exist when storing the value into a float: 1E300000 If we encode that value into html: ∞ :-D Best Wishes, -David Delaune

                      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