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. floating point comparision

floating point comparision

Scheduled Pinned Locked Moved C / C++ / MFC
help
7 Posts 5 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.
  • C Offline
    C Offline
    Cmania
    wrote on last edited by
    #1

    main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; } Well it is not working, help me out please!

    Spread wat u Know!

    D C R 3 Replies Last reply
    0
    • C Cmania

      main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; } Well it is not working, help me out please!

      Spread wat u Know!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      You cannot compare FP numbers in this fashion. You'll need something like:

      if (fabs(x - 2.4) < 0.00001)
      cout << "true" << endl;

      See here for more.


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      C 1 Reply Last reply
      0
      • D David Crow

        You cannot compare FP numbers in this fashion. You'll need something like:

        if (fabs(x - 2.4) < 0.00001)
        cout << "true" << endl;

        See here for more.


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        C Offline
        C Offline
        Cmania
        wrote on last edited by
        #3

        Thnx but I did visit Bruce Dawson But couldnt get it in nutshell Please will u?

        Spread wat u Know!

        M 1 Reply Last reply
        0
        • C Cmania

          Thnx but I did visit Bruce Dawson But couldnt get it in nutshell Please will u?

          Spread wat u Know!

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

          in short, a computer cannot "always" represent a floating point number accurately in binary format, and a precision error will be inserted when converting back and forth between the floating point number and the binary representation. for example : 2.4 can be "errored" to something like either 2.39999999 or 2.40000001


          Maximilien Lincourt Your Head A Splode - Strong Bad

          C 1 Reply Last reply
          0
          • C Cmania

            main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; } Well it is not working, help me out please!

            Spread wat u Know!

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            Cmania wrote:

            int x=2.4;

            you need to go look at the definition of "int"

            image processing toolkits | batch image processing | blogging

            1 Reply Last reply
            0
            • M Maximilien

              in short, a computer cannot "always" represent a floating point number accurately in binary format, and a precision error will be inserted when converting back and forth between the floating point number and the binary representation. for example : 2.4 can be "errored" to something like either 2.39999999 or 2.40000001


              Maximilien Lincourt Your Head A Splode - Strong Bad

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

              Thnkx a lot.............. Does this mean IEEE32/64 arent good enough to store FPs accurately AND wat when we use the same number to print on screen......i mean how is that FP printed accurately? N sorry it is actually float x=2.4;

              Spread wat u Know!

              1 Reply Last reply
              0
              • C Cmania

                main() { int x=2.4; if(x==2.4) cout<<"true"; else cout<<"false"; return 0; } Well it is not working, help me out please!

                Spread wat u Know!

                R Offline
                R Offline
                realJSOP
                wrote on last edited by
                #7

                Here's an CodeProject article that discusses this issue: http://www.codeproject.com/tips/FloatingPointEquality.asp[^]

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                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