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