Results 1 to 6 of 6
Hello linuxforums,
I know it is wrong to use the "==" operator to compare the equality of two floating point numbers.
Logically it would seem that if the "==" operator ...
- 05-10-2011 #1Just Joined!
- Join Date
- May 2011
- Posts
- 8
<= operator for floating point comparison
Hello linuxforums,
I know it is wrong to use the "==" operator to compare the equality of two floating point numbers.
Logically it would seem that if the "==" operator is not usable for floating point comparison, then the "<=" operator would also not be usable. Is this true?
The lack of google search results on this topic made me think that it must be true.
If that is true, then is it true that the only way to compare floats with <= or >= is with something like the code below?
I think this is a general question, but if specifics are necessary, I am using the C++ language to code at the moment.Code:bool smaller_than_or_equal(float a, float b) { if ( fabs(a-b) < EPSILON){return true;} else if (a < b) {return true;} else {return false;} }
Any guidance is greatly appreciated
Thanks,
Disinterred
- 05-10-2011 #2
It's not necessarily wrong to use '==', it's just that the result is not always what you'd expect, this is because of the way the number is stored in memory.
Is there a particular reason you need floating point numbers? If you're doing high-precision mathematical stuff, you may need them, otherwise why don't you just use fixed-point numbers where your fractions are represented by integers? They're much quicker to use and more accurate for comparisons.Linux user #126863 - see http://linuxcounter.net/
- 05-16-2011 #3Just Joined!
- Join Date
- May 2011
- Posts
- 8
Sorry for the lateness of my reply, I had some trouble logging in, but that is fixed now.
Thank you for your response Roxoff. I need floating point numbers because I am running a simulation where there is a bunch of particles undergoing a random walk in three dimensions. There position at some time will be described of course by the rectangular coordinates (x,y,z) where x,y,z are real numbers.
and often I seem to need a <= comparison of their positions.
thanks,
Disinterred
- 05-16-2011 #4
Well, because of the way these things are stored in memory, things can get a little complex (for example (0 != -0).
To compare 2 floats for equality, I suppose you'd rather take a margin around the floats in which they have to be for example:
I personally think this is the best way to go forward, but I'm not entirely sure, since I don't generally use floats (besides in Java, and even there I don't use them that often).Code:#include <math.h> #define EPSYLON (Your margin, eg. 0.01f) #define CMPFLT(a,b) (abs(a-b) < EPSYLON*(abs(a)+abs(b)))
(by the way, I haven't tested the code, but according to my teacher this should be just about the thing you want, because the margin scales with the size of the variables to compensate for the loss in floating point precision)
The Intel Basic Architecture Volume 1 gives more details on the way floats are stored and processed. I only skimmed over it, for it wasn't really relevant but it might be of interest to you.
- 05-17-2011 #5
Looking at the Intel Developers manual:
Might be useful if you are handy with assembly, otherwise I think the macro I wrote for you should be ok.FCOM/FCOMP/FCOMPPCompare floating point and set x87 FPU
condition code flags.
FUCOM/FUCOMP/FUCOMPPUnordered compare floating point and set
x87 FPU condition code flags.
FICOM/FICOMPCompare integer and set x87 FPU
condition code flags.
FCOMI/FCOMIPCompare floating point and set EFLAGS
status flags.
FUCOMI/FUCOMIPUnordered compare floating point and
set EFLAGS status flags.
FTST
Test (compare floating point with 0.0).
FXAMExamine.
- 05-22-2011 #6Just Joined!
- Join Date
- May 2011
- Posts
- 8


Reply With Quote
