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. Is floating point number mathematical integer?

Is floating point number mathematical integer?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • O Offline
    O Offline
    Oliver123
    wrote on last edited by
    #1

    How can one determmine if a given floating point variable is a mathematical integer (1,2,3,4,5...)? I figured I could divide by one and check for zero as a remainder. Doesn't work. float fNumber = 861; float one = 1; fmod(fNumber, one) modf(fNumber, one) Both of these return a value of .9998936354755 (or some other decimal very close to 1). I've tried various combinations of float and double, but get the same results. Ideas? Thanks.

    F C 2 Replies Last reply
    0
    • O Oliver123

      How can one determmine if a given floating point variable is a mathematical integer (1,2,3,4,5...)? I figured I could divide by one and check for zero as a remainder. Doesn't work. float fNumber = 861; float one = 1; fmod(fNumber, one) modf(fNumber, one) Both of these return a value of .9998936354755 (or some other decimal very close to 1). I've tried various combinations of float and double, but get the same results. Ideas? Thanks.

      F Offline
      F Offline
      followait
      wrote on last edited by
      #2

      It's floating point inaccuracy occured on every common pc. Control it manually. For example: float f=861; When you assin 861, it could be a little more or a little less than it, like 860.99999 or 861.00001 You can do like this: f+=0.00001f to make sure it is a little more than 861 ...

      C 1 Reply Last reply
      0
      • F followait

        It's floating point inaccuracy occured on every common pc. Control it manually. For example: float f=861; When you assin 861, it could be a little more or a little less than it, like 860.99999 or 861.00001 You can do like this: f+=0.00001f to make sure it is a little more than 861 ...

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

        followait wrote:

        float f=861; When you assin 861, it could be a little more or a little less than it,

        This is not true, the floating point standard guarantees that integer representation is perfect, so float f = 861; will result in f being 861 precisely. Integer arithmetic also is precise if it stays integral, so f = float(1722)/float(2); results in f being 861 precisely. Once your expression moves away from being able to be represented exactly then the sorts of precision errors you mention occur. e.g. f = (float(10)/float(3))*float(3); should be 10 but you will see rounding errors.


        Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

        1 Reply Last reply
        0
        • O Oliver123

          How can one determmine if a given floating point variable is a mathematical integer (1,2,3,4,5...)? I figured I could divide by one and check for zero as a remainder. Doesn't work. float fNumber = 861; float one = 1; fmod(fNumber, one) modf(fNumber, one) Both of these return a value of .9998936354755 (or some other decimal very close to 1). I've tried various combinations of float and double, but get the same results. Ideas? Thanks.

          C Offline
          C Offline
          cp9876
          wrote on last edited by
          #4

          I don't know of a simple test, but you can use floor(), ceil() or fmod() to help. e.g. if (f == floor(f)) {} if (f == ceil(f)) {} float intPart; if (fmod(f,&intPart) == 0) {} I don't know what is most efficient if speed is important.


          Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

          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