float point comparison

M

Malcolm McLean

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.
 
E

Eric Sosman

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.
 
T

Tim Prince

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.
 
B

Ben Bacarisse

Eric Sosman said:
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.
 
E

Eric Sosman

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.
 
X

x.mar

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!
 
B

BartC

x.mar said:
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!
 
T

Tim Prince

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top