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. Problem with double

Problem with double

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
6 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

    Hello, I have the following simple code and I cant understand the results I am seeing Note. This is a big hack in an old peice of code. It would not be like this if I had my way :( void SomeFn(void) { //these are not hard coded they come from another API in string form CString result = CalcMargin("0.7033", "0.7000"); } CString CalcMargin(LPCTSTR exeRate, LPCTSTR rate) { double dExeRate, dRate, dMargin; dExeRate = atof(exeRate); dRate = atof(rate); dMargin = dExeRate - dRate; result.Format("%f", dMargin); return result; } The returned value is "0.003300" and I wondered why i see the trailing zeros. It turnes out that the result of the subtraction is 0.0033000000000001 Can anyone tell me why? Thanks

    K M 2 Replies Last reply
    0
    • L Lost User

      Hello, I have the following simple code and I cant understand the results I am seeing Note. This is a big hack in an old peice of code. It would not be like this if I had my way :( void SomeFn(void) { //these are not hard coded they come from another API in string form CString result = CalcMargin("0.7033", "0.7000"); } CString CalcMargin(LPCTSTR exeRate, LPCTSTR rate) { double dExeRate, dRate, dMargin; dExeRate = atof(exeRate); dRate = atof(rate); dMargin = dExeRate - dRate; result.Format("%f", dMargin); return result; } The returned value is "0.003300" and I wondered why i see the trailing zeros. It turnes out that the result of the subtraction is 0.0033000000000001 Can anyone tell me why? Thanks

      K Offline
      K Offline
      kjessee
      wrote on last edited by
      #2

      Try this

      result.Format("%5.4lf", dMargin);

      where 5 is the minimum width and 4 is the precision. the lf is for double values.

      1 Reply Last reply
      0
      • L Lost User

        Hello, I have the following simple code and I cant understand the results I am seeing Note. This is a big hack in an old peice of code. It would not be like this if I had my way :( void SomeFn(void) { //these are not hard coded they come from another API in string form CString result = CalcMargin("0.7033", "0.7000"); } CString CalcMargin(LPCTSTR exeRate, LPCTSTR rate) { double dExeRate, dRate, dMargin; dExeRate = atof(exeRate); dRate = atof(rate); dMargin = dExeRate - dRate; result.Format("%f", dMargin); return result; } The returned value is "0.003300" and I wondered why i see the trailing zeros. It turnes out that the result of the subtraction is 0.0033000000000001 Can anyone tell me why? Thanks

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

        Rounding error. Floating-point numbers cannot be stored as the exact equivalent of their decimal representation, unless they all happen to be powers of two (2, 4, .125, etc.). So you should never check for strict equality when dealing with float/double values. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt "Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain."   -- Paul Watson, Linux Zombie

        L 1 Reply Last reply
        0
        • M Michael Dunn

          Rounding error. Floating-point numbers cannot be stored as the exact equivalent of their decimal representation, unless they all happen to be powers of two (2, 4, .125, etc.). So you should never check for strict equality when dealing with float/double values. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | 1ClickPicGrabber New v2.0! | RightClick-Encrypt "Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain."   -- Paul Watson, Linux Zombie

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

          Thanks but im not sure I understand completely. Would you expect "true" or "false" from the following code? It prints true when i run it. double d1 = 5; //not a power of 2 double d2 = 2; if( (d2 * 5.0 / 2.0) == d1) printf("true"); else printf("false");

          P 1 Reply Last reply
          0
          • L Lost User

            Thanks but im not sure I understand completely. Would you expect "true" or "false" from the following code? It prints true when i run it. double d1 = 5; //not a power of 2 double d2 = 2; if( (d2 * 5.0 / 2.0) == d1) printf("true"); else printf("false");

            P Offline
            P Offline
            PJ Arends
            wrote on last edited by
            #5

            You are using whole numbers. Try with fractions

            double d1 = 1;
            double d2 = 3;
            double d3 = d1 / d2;
            if (d3 == 0.333) // what number to use?
            printf ("true");
            else
            printf ("false);

            No matter what number you use, you will never get true.


            [

            ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

            L 1 Reply Last reply
            0
            • P PJ Arends

              You are using whole numbers. Try with fractions

              double d1 = 1;
              double d2 = 3;
              double d3 = d1 / d2;
              if (d3 == 0.333) // what number to use?
              printf ("true");
              else
              printf ("false);

              No matter what number you use, you will never get true.


              [

              ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

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

              cheers!

              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