# float point comparison

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

1. ### x.marGuest

For float points variables x and a, does "x>a" always give "x-a>0" and
vice versa?

x.mar, Oct 31, 2010

2. ### Malcolm McLeanGuest

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

3. ### Eric SosmanGuest

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
4. ### Tim PrinceGuest

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
5. ### Ben BacarisseGuest

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
6. ### Eric SosmanGuest

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
7. ### x.marGuest

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
8. ### BartCGuest

"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
9. ### Tim PrinceGuest

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