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. Other Discussions
  3. Clever Code
  4. An easy, but one of the most often coded errors

An easy, but one of the most often coded errors

Scheduled Pinned Locked Moved Clever Code
question
10 Posts 8 Posters 5 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.
  • J Offline
    J Offline
    Jun Du
    wrote on last edited by
    #1

    I put it into this routine:

    bool CheckSomething( const float x )
    {
    return (x == 5.0 ? true : false);
    }

    Best, Jun

    S C B 3 Replies Last reply
    0
    • J Jun Du

      I put it into this routine:

      bool CheckSomething( const float x )
      {
      return (x == 5.0 ? true : false);
      }

      Best, Jun

      S Offline
      S Offline
      Stephan Pilz
      wrote on last edited by
      #2

      I think you would write return (x == 5.0) ? true : false; Right? But I'm interesting for the return value of your code. I riddle at the moment, if it returns true or false. Stephan

                     \\\\\\|///
                   \\\\  - -  //
                    (  @ @  )
      

      +---------------oOOo-(_)-oOOo-----------------+
      | Stephan Pilz stephan.pilz@stephan-pilz.de |
      | www.stephan-pilz.de |
      | ICQ#: 127823481 |
      +-----------------------Oooo------------------+
      oooO ( )
      ( ) ) /
      \ ( (_/
      \_)

      M 1 Reply Last reply
      0
      • S Stephan Pilz

        I think you would write return (x == 5.0) ? true : false; Right? But I'm interesting for the return value of your code. I riddle at the moment, if it returns true or false. Stephan

                       \\\\\\|///
                     \\\\  - -  //
                      (  @ @  )
        

        +---------------oOOo-(_)-oOOo-----------------+
        | Stephan Pilz stephan.pilz@stephan-pilz.de |
        | www.stephan-pilz.de |
        | ICQ#: 127823481 |
        +-----------------------Oooo------------------+
        oooO ( )
        ( ) ) /
        \ ( (_/
        \_)

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

        Stephan Pilz wrote:

        Right?

        wrong, You cannot compare float numbers like that, their internal representation will always ( more or less ) be different than what the real number is. X might be 5.0, but will more likelly be 4.99999999 or 5.00000001. so you will have to compare against a tolerance value.

        if ( fabs( x-5.0 ) < 0.0000001 )
        return true;
        else
        return false;

        or something like that.


        Maximilien Lincourt Your Head A Splode - Strong Bad

        S R 2 Replies Last reply
        0
        • M Maximilien

          Stephan Pilz wrote:

          Right?

          wrong, You cannot compare float numbers like that, their internal representation will always ( more or less ) be different than what the real number is. X might be 5.0, but will more likelly be 4.99999999 or 5.00000001. so you will have to compare against a tolerance value.

          if ( fabs( x-5.0 ) < 0.0000001 )
          return true;
          else
          return false;

          or something like that.


          Maximilien Lincourt Your Head A Splode - Strong Bad

          S Offline
          S Offline
          Stephan Pilz
          wrote on last edited by
          #4

          Maximilien wrote:

          You cannot compare float numbers like that

          No. I can compare. 5.0 and 5.000000 are alwasy the same representation in memory. If you would compare values from your example, you are right, but 5.0 is 5.000000 is 5.000 !! Ever. About the sense of the comparison can be argued of course. Stephan

                         \\\\\\|///
                       \\\\  - -  //
                        (  @ @  )
          

          +---------------oOOo-(_)-oOOo-----------------+
          | Stephan Pilz stephan.pilz@stephan-pilz.de |
          | www.stephan-pilz.de |
          | ICQ#: 127823481 |
          +-----------------------Oooo------------------+
          oooO ( )
          ( ) ) /
          \ ( (_/
          \_)

          C 1 Reply Last reply
          0
          • J Jun Du

            I put it into this routine:

            bool CheckSomething( const float x )
            {
            return (x == 5.0 ? true : false);
            }

            Best, Jun

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

            There should be a flag in compilers that stops you trying to do equality checks on floats.

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            J 1 Reply Last reply
            0
            • S Stephan Pilz

              Maximilien wrote:

              You cannot compare float numbers like that

              No. I can compare. 5.0 and 5.000000 are alwasy the same representation in memory. If you would compare values from your example, you are right, but 5.0 is 5.000000 is 5.000 !! Ever. About the sense of the comparison can be argued of course. Stephan

                             \\\\\\|///
                           \\\\  - -  //
                            (  @ @  )
              

              +---------------oOOo-(_)-oOOo-----------------+
              | Stephan Pilz stephan.pilz@stephan-pilz.de |
              | www.stephan-pilz.de |
              | ICQ#: 127823481 |
              +-----------------------Oooo------------------+
              oooO ( )
              ( ) ) /
              \ ( (_/
              \_)

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Stephan Pilz wrote:

              No. I can compare. 5.0 and 5.000000 are alwasy the same representation in memory. If you would compare values from your example, you are right, but 5.0 is 5.000000 is 5.000 !! Ever.

              But he is not comparing 5.0 with 5.00000. He is comparing x with 5.0. X may be the result of (100.0 / 20.0) which may contain a rounding error. Therefore you have to build in a tolerance. Usually expressed as Epsilon.


              Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

              1 Reply Last reply
              0
              • J Jun Du

                I put it into this routine:

                bool CheckSomething( const float x )
                {
                return (x == 5.0 ? true : false);
                }

                Best, Jun

                B Offline
                B Offline
                Bugra Barin
                wrote on last edited by
                #7

                Sadly, I've seen this done way too many times even by "experienced" programmers.

                1 Reply Last reply
                0
                • C Chris Maunder

                  There should be a flag in compilers that stops you trying to do equality checks on floats.

                  cheers, Chris Maunder

                  CodeProject.com : C++ MVP

                  J Offline
                  J Offline
                  Jun Du
                  wrote on last edited by
                  #8

                  Agree on that. A warning should be sufficient, but I suspect no compilers do so.

                  Best, Jun

                  R 1 Reply Last reply
                  0
                  • J Jun Du

                    Agree on that. A warning should be sufficient, but I suspect no compilers do so.

                    Best, Jun

                    R Offline
                    R Offline
                    rollei35guy
                    wrote on last edited by
                    #9

                    There used to be a warning indicating that "equality comparisions between non-integers may not be meaningful" or something like that. It might have been in an old Fortran compiler though.

                    1 Reply Last reply
                    0
                    • M Maximilien

                      Stephan Pilz wrote:

                      Right?

                      wrong, You cannot compare float numbers like that, their internal representation will always ( more or less ) be different than what the real number is. X might be 5.0, but will more likelly be 4.99999999 or 5.00000001. so you will have to compare against a tolerance value.

                      if ( fabs( x-5.0 ) < 0.0000001 )
                      return true;
                      else
                      return false;

                      or something like that.


                      Maximilien Lincourt Your Head A Splode - Strong Bad

                      R Offline
                      R Offline
                      Richard Parsons
                      wrote on last edited by
                      #10

                      And better yet... return ( fabs( x-5.0 ) < 0.0000001 );

                      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