float point comparison

Discussion in 'C Programming' started by x.mar, Oct 31, 2010.

  1. x.mar

    x.mar Guest

    For float points variables x and a, does "x>a" always give "x-a>0" and
    vice versa?
    x.mar, Oct 31, 2010
    #1
    1. Advertising

  2. On Oct 31, 4:19 pm, "x.mar" <> wrote:
    > For float points variables x and a, does "x>a" always give "x-a>0" and
    > vice versa?
    >

    Yes, as far as I know, but you shouldn't rely on it. I think every
    floating point format in use allows x-a to be represented as non-zero
    if x differs from a, even if it's only by one bit. However x - a will
    not necessarily be bigger than x.
    Malcolm McLean, Oct 31, 2010
    #2
    1. Advertising

  3. x.mar

    Eric Sosman Guest

    On 10/31/2010 10:19 AM, x.mar wrote:
    > For float points variables x and a, does "x>a" always give "x-a>0" and
    > vice versa?


    For finite floats, I think so. However, if x is Inf and a is -Inf,
    x>a is true but x-a == NaN and NaN > 0 is false.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 31, 2010
    #3
  4. x.mar

    Tim Prince Guest

    On 10/31/2010 7:19 AM, x.mar wrote:
    > For float points variables x and a, does "x>a" always give "x-a>0" and
    > vice versa?

    Not when abrupt underflow is set, if I can make assumptions about your
    meaning. This is a reason for options provided by many compilers to set
    compliance or not with IEEE754 standards.

    --
    Tim Prince
    Tim Prince, Oct 31, 2010
    #4
  5. Eric Sosman <> writes:

    > On 10/31/2010 10:19 AM, x.mar wrote:
    >> For float points variables x and a, does "x>a" always give "x-a>0" and
    >> vice versa?

    >
    > For finite floats, I think so. However, if x is Inf and a is -Inf,
    > x>a is true but x-a == NaN and NaN > 0 is false.


    I think (though I don't have the standard handy) that Inf - (-Inf) ==
    Inf rather than NaN in IEEE floating-point arithmetic, and it seems a
    sounds choice for any FP implementation.

    --
    Ben.
    Ben Bacarisse, Oct 31, 2010
    #5
  6. x.mar

    Eric Sosman Guest

    On 10/31/2010 6:43 PM, Ben Bacarisse wrote:
    > Eric Sosman<> writes:
    >
    >> On 10/31/2010 10:19 AM, x.mar wrote:
    >>> For float points variables x and a, does "x>a" always give "x-a>0" and
    >>> vice versa?

    >>
    >> For finite floats, I think so. However, if x is Inf and a is -Inf,
    >> x>a is true but x-a == NaN and NaN> 0 is false.

    >
    > I think (though I don't have the standard handy) that Inf - (-Inf) ==
    > Inf rather than NaN in IEEE floating-point arithmetic, and it seems a
    > sounds choice for any FP implementation.


    Sorry; brain hiccup. You're right.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 1, 2010
    #6
  7. x.mar

    x.mar Guest

    On Oct 31, 10:19 am, "x.mar" <> wrote:
    > For float points variables x and a, does "x>a" always give "x-a>0" and
    > vice versa?


    Thank all for the replies. I encountered this question when I am
    implementing an image analysis algorithm where I need to inspect a
    line segment over an image pixel by pixel. So the question should be
    looked at in the context of such; the range of x and a will span no
    more than 4-5 order of magnitude, typically 0.1-1000.0. We can safely
    assume situations such INF and NaN are safely guarded elsewhere.

    With this restriction in place can I safely assume x-a>0 will be true
    if x>a is true?

    Thanks!
    x.mar, Nov 1, 2010
    #7
  8. x.mar

    BartC Guest

    "x.mar" <> wrote in message
    news:...
    > On Oct 31, 10:19 am, "x.mar" <> wrote:
    >> For float points variables x and a, does "x>a" always give "x-a>0" and
    >> vice versa?

    >
    > Thank all for the replies. I encountered this question when I am
    > implementing an image analysis algorithm where I need to inspect a
    > line segment over an image pixel by pixel. So the question should be
    > looked at in the context of such; the range of x and a will span no
    > more than 4-5 order of magnitude, typically 0.1-1000.0. We can safely
    > assume situations such INF and NaN are safely guarded elsewhere.
    >
    > With this restriction in place can I safely assume x-a>0 will be true
    > if x>a is true?
    >
    > Thanks!
    BartC, Nov 1, 2010
    #8
  9. x.mar

    Tim Prince Guest

    On 11/1/2010 1:41 PM, x.mar wrote:
    > On Oct 31, 10:19 am, "x.mar"<> wrote:
    >> For float points variables x and a, does "x>a" always give "x-a>0" and
    >> vice versa?

    >
    > Thank all for the replies. I encountered this question when I am
    > implementing an image analysis algorithm where I need to inspect a
    > line segment over an image pixel by pixel. So the question should be
    > looked at in the context of such; the range of x and a will span no
    > more than 4-5 order of magnitude, typically 0.1-1000.0. We can safely
    > assume situations such INF and NaN are safely guarded elsewhere.
    >
    > With this restriction in place can I safely assume x-a>0 will be true
    > if x>a is true?

    The problem related to abrupt underflow will bite you only when
    fabs(x-a) < DBL_EPSILON (FLT_EPSILON when both x and a are float
    types). You are safe even from that possibility if your minimum values
    are 0.1 or 0.1f. IEEE754 assures better properties for |x| > 0.1 than
    for smaller values. AFAIK this is outside the purview of standard C,
    but for practical purposes it applies to all architectures introduced
    since standard C came about.

    --
    Tim Prince
    Tim Prince, Nov 2, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    597
  2. prashna

    Float point comparison-Newbie

    prashna, Nov 11, 2004, in forum: C Programming
    Replies:
    11
    Views:
    779
    Flash Gordon
    Nov 12, 2004
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,486
    James Kanze
    Oct 8, 2009
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,275
    Saraswati lakki
    Jan 6, 2012
  5. Deepu
    Replies:
    1
    Views:
    213
    ccc31807
    Feb 7, 2011
Loading...

Share This Page