Velocity Reviews > float point comparison

# float point comparison

x.mar
Guest
Posts: n/a

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

Malcolm McLean
Guest
Posts: n/a

 10-31-2010
On Oct 31, 4:19*pm, "x.mar" <(E-Mail Removed)> 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.

Eric Sosman
Guest
Posts: n/a

 10-31-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

Tim Prince
Guest
Posts: n/a

 10-31-2010
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

Ben Bacarisse
Guest
Posts: n/a

 10-31-2010
Eric Sosman <(E-Mail Removed)> 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.

Eric Sosman
Guest
Posts: n/a

 11-01-2010
On 10/31/2010 6:43 PM, Ben Bacarisse wrote:
> Eric Sosman<(E-Mail Removed)> 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
(E-Mail Removed)lid

x.mar
Guest
Posts: n/a

 11-01-2010
On Oct 31, 10:19*am, "x.mar" <(E-Mail Removed)> 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
Guest
Posts: n/a

 11-01-2010

"x.mar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Oct 31, 10:19 am, "x.mar" <(E-Mail Removed)> 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!

Tim Prince
Guest
Posts: n/a

 11-02-2010
On 11/1/2010 1:41 PM, x.mar wrote:
> On Oct 31, 10:19 am, "x.mar"<(E-Mail Removed)> 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

--
Tim Prince