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. Weird <= operation

Weird <= operation

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
8 Posts 6 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
    alex1205
    wrote on last edited by
    #1

    Hi, I have encountered a problem with a very basic operation. does anybody have any idea about this? Please see sample code below: ////////////////////////////////////////// double a, b; a = 99.99 b = 99.99 if (a <= b){ ::MessageBox(NULL, L"TRUE", L"", MB_OK); } else{ ::MessageBox(NULL, L"FALSE", L"", MB_OK); } ////////////////////////////////////////// somehow, it always display FALSE. Anybody knows how to handle this situation? SDE

    B P 2 Replies Last reply
    0
    • A alex1205

      Hi, I have encountered a problem with a very basic operation. does anybody have any idea about this? Please see sample code below: ////////////////////////////////////////// double a, b; a = 99.99 b = 99.99 if (a <= b){ ::MessageBox(NULL, L"TRUE", L"", MB_OK); } else{ ::MessageBox(NULL, L"FALSE", L"", MB_OK); } ////////////////////////////////////////// somehow, it always display FALSE. Anybody knows how to handle this situation? SDE

      B Offline
      B Offline
      basementman
      wrote on last edited by
      #2

      Doubles are NOT precise numbers. They are approximate representations. Whenever you need to compare doubles (or check if zero), you need to do approximate math, using an acceptable number of precision digits to your app. An example is below:

      BOOL IsDoubleZero(double dVal)
      {
      if (dVal < 0.000001 && dVal > -0.000001)
      return TRUE;

      return FALSE;
      }

      BOOL AreDoublesEqual(double dVal1, double dVal2)
      {
      doubld dDiff = dVal1 - dVal2;
      if (dDiff < 0.000001 && dDiff > -0.000001)
      return TRUE;

      return FALSE;
      }

      onwards and upwards...

      M A 2 Replies Last reply
      0
      • B basementman

        Doubles are NOT precise numbers. They are approximate representations. Whenever you need to compare doubles (or check if zero), you need to do approximate math, using an acceptable number of precision digits to your app. An example is below:

        BOOL IsDoubleZero(double dVal)
        {
        if (dVal < 0.000001 && dVal > -0.000001)
        return TRUE;

        return FALSE;
        }

        BOOL AreDoublesEqual(double dVal1, double dVal2)
        {
        doubld dDiff = dVal1 - dVal2;
        if (dDiff < 0.000001 && dDiff > -0.000001)
        return TRUE;

        return FALSE;
        }

        onwards and upwards...

        M Offline
        M Offline
        Malcolm Smart
        wrote on last edited by
        #3

        Wow - never knew that. What about floats? Same thing? Hope so else I just screwed up an interview question! ********************************************* The sooner you fall behind, the longer you have to catch up.

        RaviBeeR 1 Reply Last reply
        0
        • M Malcolm Smart

          Wow - never knew that. What about floats? Same thing? Hope so else I just screwed up an interview question! ********************************************* The sooner you fall behind, the longer you have to catch up.

          RaviBeeR Offline
          RaviBeeR Offline
          RaviBee
          wrote on last edited by
          #4

          Angel1058 wrote: What about floats? Same thing? Yes. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

          M 1 Reply Last reply
          0
          • RaviBeeR RaviBee

            Angel1058 wrote: What about floats? Same thing? Yes. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #5

            I find that absolutely astonishing - you can't compare two doubles or floats for equality? So all these financial and scientific calculations using massive numbers with loads of decimal places aren't accurate - they are approximations?!? It's not that I am doubting anything written here but I still find that amazing. 99.99 isn't guaranteed to equal 99.99. Are there any precise types then? ********************************************* The sooner you fall behind, the longer you have to catch up.

            RaviBeeR 1 Reply Last reply
            0
            • M Malcolm Smart

              I find that absolutely astonishing - you can't compare two doubles or floats for equality? So all these financial and scientific calculations using massive numbers with loads of decimal places aren't accurate - they are approximations?!? It's not that I am doubting anything written here but I still find that amazing. 99.99 isn't guaranteed to equal 99.99. Are there any precise types then? ********************************************* The sooner you fall behind, the longer you have to catch up.

              RaviBeeR Offline
              RaviBeeR Offline
              RaviBee
              wrote on last edited by
              #6

              Angel1058 wrote: So all these financial and scientific calculations ... they are approximations?!? Correct. Most scientific and financial applications (including my hack TakeStock[^] (forgive the shameless plug - but after all, it is freeware) will instead store floating point numbers as an aggregation of integers (which can be accurately compared). So a bank balance of $12,345,678.90123 will instead be stored as the integer value 1,234,567,890,123 "milli cents". Since the number of mCents could be extremely large, you need store this value in a collection of ints. In the 70's and early 80's, many programmers used PL/1 which included a DECIMAL data structure for exactly this kind of thing. Microsoft provides the CURRENCY[^] type that suffices for most financial calculations. Hope this helps! /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

              1 Reply Last reply
              0
              • A alex1205

                Hi, I have encountered a problem with a very basic operation. does anybody have any idea about this? Please see sample code below: ////////////////////////////////////////// double a, b; a = 99.99 b = 99.99 if (a <= b){ ::MessageBox(NULL, L"TRUE", L"", MB_OK); } else{ ::MessageBox(NULL, L"FALSE", L"", MB_OK); } ////////////////////////////////////////// somehow, it always display FALSE. Anybody knows how to handle this situation? SDE

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                Is this your actual code? What cmpiler, what settings do you use? While it is true what was said - floats and doubles are not accurate representations - a simple assignment like yours typically is ok. (And your ode with default debug/release settings outputs "TRUE" for me). float and double both use a limited number of digits, certain numbers e.g 1/3, cannot berepresented accurately this way. Also, during calculations, final digits simply get lost. So comparing *results* as replied is a must. However, you should also consider the alternative:

                bool IsEqualRel(double a, double b, double eps = 1e-5)
                {
                _ASSERTE(a!= 0 && b != 0);
                if (a==b) return true;
                if (a<0 != b<0) return false;
                return fabs((a-b)/a+b)) < eps;
                }

                bool IsZero(double a, double eps = 1e-5)
                {
                return a==0 || fabs(a) < eps;
                }

                which works for all magnitudes except comparison to zero (that's why the separate IsZero function). With the absolute posted by the other guy, 1e-27 == 100e-27 and 10000000000000 != 10000000000001 which is often not what you need when working with numbers in this range.


                I never really know a killer from a savior
                boost your code || Fold With Us! || sighist | doxygen

                1 Reply Last reply
                0
                • B basementman

                  Doubles are NOT precise numbers. They are approximate representations. Whenever you need to compare doubles (or check if zero), you need to do approximate math, using an acceptable number of precision digits to your app. An example is below:

                  BOOL IsDoubleZero(double dVal)
                  {
                  if (dVal < 0.000001 && dVal > -0.000001)
                  return TRUE;

                  return FALSE;
                  }

                  BOOL AreDoublesEqual(double dVal1, double dVal2)
                  {
                  doubld dDiff = dVal1 - dVal2;
                  if (dDiff < 0.000001 && dDiff > -0.000001)
                  return TRUE;

                  return FALSE;
                  }

                  onwards and upwards...

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  Thanks guys. These have been a great help. I've been thinking about this for quite some time now.

                  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