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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. type casting float to int/short

type casting float to int/short

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingquestion
10 Posts 4 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
    acerunner316
    wrote on last edited by
    #1

    I ran into a bug in my app and eventually figured that it was a problem in the type casting. I wrote a short piece of code to confirm it. float tempflt = 3.8; int tempint = (int)(tempflt*100); CString Debug; Debug.Format("%d, %f", tempint, tempflt); MessageBox(Debug); Why is it that tempint becomes 379 and not 380?

    C R M 3 Replies Last reply
    0
    • A acerunner316

      I ran into a bug in my app and eventually figured that it was a problem in the type casting. I wrote a short piece of code to confirm it. float tempflt = 3.8; int tempint = (int)(tempflt*100); CString Debug; Debug.Format("%d, %f", tempint, tempflt); MessageBox(Debug); Why is it that tempint becomes 379 and not 380?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      A floating point value is never precise.  Sorry, that's how it goes.  Try including math.h and do this: int tempint = (int )ceil(tempflt*100); I think that's the right syntax, but the Math class has floor and ceiling methods, which round up and down.  This will round your 379.xxx up to 380.00, then you cast it to int.

      Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

      A 1 Reply Last reply
      0
      • C Christian Graus

        A floating point value is never precise.  Sorry, that's how it goes.  Try including math.h and do this: int tempint = (int )ceil(tempflt*100); I think that's the right syntax, but the Math class has floor and ceiling methods, which round up and down.  This will round your 379.xxx up to 380.00, then you cast it to int.

        Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

        A Offline
        A Offline
        acerunner316
        wrote on last edited by
        #3

        Christian Graus wrote:

        A floating point value is never precise.

        How predictable is a floats actual value? Because I don't think ceil will be a general solution. What about cases when I should be rounding down? Is double more precise? I really don't need too much precision as far as decimal points goes. The range I will be in will be -32.768 to 32.767 (the range of short divided by 1000), so I only need 3 numbers after the decimal, which is why i chose float instead of double. -- modified at 19:35 Tuesday 16th January, 2007

        C 1 Reply Last reply
        0
        • A acerunner316

          I ran into a bug in my app and eventually figured that it was a problem in the type casting. I wrote a short piece of code to confirm it. float tempflt = 3.8; int tempint = (int)(tempflt*100); CString Debug; Debug.Format("%d, %f", tempint, tempflt); MessageBox(Debug); Why is it that tempint becomes 379 and not 380?

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

          This is because the float (or double) to int cast performs a truncated conversion. The actual answer was probably more like 379.9999999999999999 but the truncation gives a result of 379. If you want a rounded answer then do this : int tempint = (int)( tempflt * 100 + 0.5 );

          A 1 Reply Last reply
          0
          • A acerunner316

            Christian Graus wrote:

            A floating point value is never precise.

            How predictable is a floats actual value? Because I don't think ceil will be a general solution. What about cases when I should be rounding down? Is double more precise? I really don't need too much precision as far as decimal points goes. The range I will be in will be -32.768 to 32.767 (the range of short divided by 1000), so I only need 3 numbers after the decimal, which is why i chose float instead of double. -- modified at 19:35 Tuesday 16th January, 2007

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            acerunner316 wrote:

            What about cases when I should be rounding down?

            floor

            acerunner316 wrote:

            Is double more precise?

            Yes, but not exact.

            Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

            A 1 Reply Last reply
            0
            • C Christian Graus

              acerunner316 wrote:

              What about cases when I should be rounding down?

              floor

              acerunner316 wrote:

              Is double more precise?

              Yes, but not exact.

              Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

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

              Christian Graus wrote:

              acerunner316 wrote: What about cases when I should be rounding down? floor

              That's my point. I need a generic code that will accept a float and multiply by 100 and get the right integer. Without knowing how float will skew the result of the multiply operation, I won't know when ot use ceil, and when to use floor.

              C 1 Reply Last reply
              0
              • R Rick York

                This is because the float (or double) to int cast performs a truncated conversion. The actual answer was probably more like 379.9999999999999999 but the truncation gives a result of 379. If you want a rounded answer then do this : int tempint = (int)( tempflt * 100 + 0.5 );

                A Offline
                A Offline
                acerunner316
                wrote on last edited by
                #7

                That's a good idea. :) As long as the float math operations don't skew the answers by more than 0.5, which it doesn't seem like it will do.

                C 1 Reply Last reply
                0
                • A acerunner316

                  That's a good idea. :) As long as the float math operations don't skew the answers by more than 0.5, which it doesn't seem like it will do.

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  They won't, that always guarentees you'll get what you want. ( I forgot that little trick )

                  Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                  1 Reply Last reply
                  0
                  • A acerunner316

                    Christian Graus wrote:

                    acerunner316 wrote: What about cases when I should be rounding down? floor

                    That's my point. I need a generic code that will accept a float and multiply by 100 and get the right integer. Without knowing how float will skew the result of the multiply operation, I won't know when ot use ceil, and when to use floor.

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    you always would need ceil.  Just like adding .5 assumes that you need to round up.

                    Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                    1 Reply Last reply
                    0
                    • A acerunner316

                      I ran into a bug in my app and eventually figured that it was a problem in the type casting. I wrote a short piece of code to confirm it. float tempflt = 3.8; int tempint = (int)(tempflt*100); CString Debug; Debug.Format("%d, %f", tempint, tempflt); MessageBox(Debug); Why is it that tempint becomes 379 and not 380?

                      M Offline
                      M Offline
                      malaugh
                      wrote on last edited by
                      #10

                      The best method of making sure you get the correct value is to add 0.5 before casting.

                      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