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. Bug in Vicual C++ or programming mistake ?

Bug in Vicual C++ or programming mistake ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
7 Posts 3 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

    long h3 double h2; float h4; h4=4.35*100*150/100.0; //float h3=(long)(h4+0.5); //=653 ! h2=4.35*100*150/100.0; //double h3=(long)(h2+0.5);//=652 ! What is wrong in the source sample ? Bug casting ? Illegal casting ? Tanks for your answers.

    J R 2 Replies Last reply
    0
    • L Lost User

      long h3 double h2; float h4; h4=4.35*100*150/100.0; //float h3=(long)(h4+0.5); //=653 ! h2=4.35*100*150/100.0; //double h3=(long)(h2+0.5);//=652 ! What is wrong in the source sample ? Bug casting ? Illegal casting ? Tanks for your answers.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after long h3) and I only get a warning message in the assignment to h4: warning C4305: '=' : truncation from 'const double' to 'float' Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      L 3 Replies Last reply
      0
      • J Joaquin M Lopez Munoz

        What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after long h3) and I only get a warning message in the assignment to h4: warning C4305: '=' : truncation from 'const double' to 'float' Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

        Sorry for the missing ";"... I know the warning... but I'm very surprised it works in VC 5... I use VC6... I'm going to install my old VC 5 to see if I have the right result. Thanks. Seb.

        1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after long h3) and I only get a warning message in the assignment to h4: warning C4305: '=' : truncation from 'const double' to 'float' Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

          Are we talking about the same thing ? I have no problem to compile this code. What I am searching is why the result is different between the 2 lines (652 != 653)...

          J 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            What version of VC++ are you using? Is this all the relevant code? On VC++ 5.0 your code compiles fine (after adding a missing ';' after long h3) and I only get a warning message in the assignment to h4: warning C4305: '=' : truncation from 'const double' to 'float' Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

            I have installed a VC 5 : same problem (652 vs 653) Can you test this : h4=2.67*100*150/100.0; //float h3=(long)(h4+0.5); //= 401 ??? h2=2.67*100*150/100.0; //double h3=(long)(h2+0.5);//= 401 ??? I suspect an error on some type of configuration... Thank your for your answer... Seb.

            1 Reply Last reply
            0
            • L Lost User

              long h3 double h2; float h4; h4=4.35*100*150/100.0; //float h3=(long)(h4+0.5); //=653 ! h2=4.35*100*150/100.0; //double h3=(long)(h2+0.5);//=652 ! What is wrong in the source sample ? Bug casting ? Illegal casting ? Tanks for your answers.

              R Offline
              R Offline
              rob bakes
              wrote on last edited by
              #6

              The double h2=0.5+ (4.35*100*150/100.0); isn't actually 653 but looks like it is 652.9999999999999 , hence the cast truncate returns 652. printf and the IDE debugger don't show this. we can see by adding this code if ((653.0-h2)==0) printf ("equal\n"); also by adding .0000000000001 to h2 before the cast, it then becomes 653 as expected. The float value must be truncating somewhere and avoiding the rounding error. . (guessing)

              1 Reply Last reply
              0
              • L Lost User

                Are we talking about the same thing ? I have no problem to compile this code. What I am searching is why the result is different between the 2 lines (652 != 653)...

                J Offline
                J Offline
                Joaquin M Lopez Munoz
                wrote on last edited by
                #7

                Oh, I completely misunderstood your first point. Sorry. There's nothing wrong with your code. The strange behavior is due to rounding errors inherent to floating point arithmetic. These numbers are stored in a internal format like this: s*mantissa*2^exponent Where s is the sign (+1 or -1) and mantissa is a number between 1 and 2, with a fixed number of decimals. Due to the limited amount of decimals (binary decimals, to be exact) in mantissa, some numbers that in base 10 have a finite number of decimals can only be approximated by a float or a double. Also, these two types reserve differents amounts of memory for representation: floats are 4 bytes wide and doubles take 8 bytes, so these latter are more precise although this is not always the case --keep on reading. In your specific case, I've taken a look at the internal representation of h4 and h2 and the exact values are: h4 =         10690560 * 2^-14 = 652.5 h2 = 5739450696990719 * 2^-43 ~ 652.49999999999988631316227838... and from this is obvious to deduce why you obtain different results when adding 0.5 and truncating to long. If you want to know more about floating point arithmetic, do a search on Internet for "IEEE 754" (the standard most microprocessors are based upon for this kind of things). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                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