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. 10 & 10/3*3 are not equal ! ! ! WHY?

10 & 10/3*3 are not equal ! ! ! WHY?

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

    Hello all and congratulate for new theme of site.

    float f1,f2;
    bool b;
    float f1 = 10.0;
    float f2 = f1 / 3;
    b = f1 == (f2 * 3.0);

    WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .

    M N E A J 5 Replies Last reply
    0
    • H hasani2007

      Hello all and congratulate for new theme of site.

      float f1,f2;
      bool b;
      float f1 = 10.0;
      float f2 = f1 / 3;
      b = f1 == (f2 * 3.0);

      WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .

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

      Because when you deal with floating point values, equality cannot be expressed. because one of the number cannot be accurately be represented as a binary number thus leading to rounding errors that should be taken into account by the developers. see this : http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]

      Watched code never compiles.

      N 1 Reply Last reply
      0
      • H hasani2007

        Hello all and congratulate for new theme of site.

        float f1,f2;
        bool b;
        float f1 = 10.0;
        float f2 = f1 / 3;
        b = f1 == (f2 * 3.0);

        WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        Most numbers cannot be stored accurately in a float. Those numbers have to be rounded off to some near representation, 10/3 being one. Nothing to be upset about. It's just another day at the office for your computer. Read more here[^].

        home

        1 Reply Last reply
        0
        • H hasani2007

          Hello all and congratulate for new theme of site.

          float f1,f2;
          bool b;
          float f1 = 10.0;
          float f2 = f1 / 3;
          b = f1 == (f2 * 3.0);

          WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .

          E Offline
          E Offline
          EverettJF
          wrote on last edited by
          #4

          i remember,when i was a student, my c language teacher told me , if i compare two "float" or "double", i should check it between a very little number. such as , const float c_tmp = 0.000001; bool b; float f1 = 10.0; float f2 = f1 / 3; b = (f1 >= (f2 * 3.0) - c_tmp) && (f1 <= (f2 * 3.0) + c_tmp);

          R 1 Reply Last reply
          0
          • E EverettJF

            i remember,when i was a student, my c language teacher told me , if i compare two "float" or "double", i should check it between a very little number. such as , const float c_tmp = 0.000001; bool b; float f1 = 10.0; float f2 = f1 / 3; b = (f1 >= (f2 * 3.0) - c_tmp) && (f1 <= (f2 * 3.0) + c_tmp);

            R Offline
            R Offline
            Rick York
            wrote on last edited by
            #5

            or you could write double difference = f1 - ( f2 * 3.0 ); bool b = fabs( difference ) < epsilon; I think that is a little more self-explanatory plus it is faster since the calculations and comparisons are only performed once.

            1 Reply Last reply
            0
            • H hasani2007

              Hello all and congratulate for new theme of site.

              float f1,f2;
              bool b;
              float f1 = 10.0;
              float f2 = f1 / 3;
              b = f1 == (f2 * 3.0);

              WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              10 divided by 3 is 3.333 recurring in decimal and in binary it's something like: 11.01010101 (can't be bothered working it out any further). Say you have a computer which stores floats as 6 bits for the non-exponent chunk of the number. It'd truncate the value of 10/3 to 11.0101. When you multiply it by three you get: 1001.1111 which is truncated to 1001.11 which is 9.75 in decimal. While real double precision numbers use oodles more bits to represent the non-exponential part you get the same problem. Note that you've got a similar problem with order of evaluation: (A * B ) / C for a computer using a finite number of bits is not necessarily equal to ( A / C ) * B. Fortunately most real world uses of computers don't have these problems - it's really only a concern to people doing simulation or some science. The rest of us can use integers most of the time and get away with it. Cheers, Ash

              1 Reply Last reply
              0
              • M Maximilien

                Because when you deal with floating point values, equality cannot be expressed. because one of the number cannot be accurately be represented as a binary number thus leading to rounding errors that should be taken into account by the developers. see this : http://docs.sun.com/source/806-3568/ncg_goldberg.html[^]

                Watched code never compiles.

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #7

                Maximilien wrote:

                Because when you deal with floating point values, equality cannot be expressed.

                Comparisons work very well with floating point numbers, e.g. equality. Arithmetics, on the other hand, is tampering with accuracy.

                home

                1 Reply Last reply
                0
                • H hasani2007

                  Hello all and congratulate for new theme of site.

                  float f1,f2;
                  bool b;
                  float f1 = 10.0;
                  float f2 = f1 / 3;
                  b = f1 == (f2 * 3.0);

                  WHY always b is 0 (false). Really aren't f1 and f2 equal? If it is, why? THankS in AdVanCE. . .

                  J Offline
                  J Offline
                  josda1000
                  wrote on last edited by
                  #8

                  In the code, you do this: float f2 = f1 / 3; I believe you're looking for this: fload f2 = f1 / 3.0; I'm not sure it's going to set b = true, however, it's more correct nontheless.

                  Josh Davis
                  This is what plays in my head when I finish projects.

                  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