Find the answer to your Linux question:
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 ...
  1. #1
    Just 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?

    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;} 
    
    }
    I think this is a general question, but if specifics are necessary, I am using the C++ language to code at the moment.

    Any guidance is greatly appreciated

    Thanks,
    Disinterred

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    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/

  3. #3
    Just 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

  4. #4
    Linux Enthusiast Bemk's Avatar
    Join Date
    Sep 2008
    Location
    Oosterhout-NB, Netherlands
    Posts
    522
    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:

    Code:
    #include <math.h>
    #define EPSYLON (Your margin, eg. 0.01f)
    #define CMPFLT(a,b) (abs(a-b) < EPSYLON*(abs(a)+abs(b)))
    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).

    (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.
    Full time computer science student, spare time OS developer.
    @bemk92 on twitter.

  5. #5
    Linux Enthusiast Bemk's Avatar
    Join Date
    Sep 2008
    Location
    Oosterhout-NB, Netherlands
    Posts
    522
    Looking at the Intel Developers manual:
    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.
    Might be useful if you are handy with assembly, otherwise I think the macro I wrote for you should be ok.
    Full time computer science student, spare time OS developer.
    @bemk92 on twitter.

  6. #6
    Just Joined!
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by Bemk View Post
    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.
    Thank you for your reply Bemk. I think you and Roxoff have clearly shown me that the <= cannot be trusted. I will resort to other measures.

    Cheers,
    Disinterred

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...