Is "a >= b" equivalent to "a - b >= 0"?

H

hjbortol

Hi!

Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++?
Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler
dependent?

Any references are welcome!

Thanks in advance, Humberto.
 
M

mschellens

It is not the same.

Consider (a-b) is smaller than the smallest possible value.
A special case of this are unsigned types.

marc
 
R

Risto Lankinen

Hi!

Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++?
Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler
dependent?

Any references are welcome!

In C, consider what happens if e.g. b==MIN_INT .

In C++, consider that any operator could be overloaded, and that
different [set of] operators is called in each case.

- Risto -
 
J

Jirka Klaue

Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++?
Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler
dependent?

If a and b are unsigned, then of course : No!

#include <stdio.h>
int main()
{
unsigned a = 1, b = 2;
printf("%d %c= %d\n", a >= b, "!="[(a >= b) == (a - b >= 0)], a - b >=
0);
}

and even if a and b are ints, the answer is: No!

#include <stdio.h>
#include <limits.h>
int main()
{
int a = INT_MIN, b = INT_MAX;
printf("%d %c= %d\n", a >= b, "!="[(a >= b) == (a - b >= 0)], a - b >=
0);
}

Jirka
 
V

Victor Bazarov

Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++?

There is no such thing as C/C++. And AFA C++ is concerned, no, the
expressions are not equivalent. 'a' and 'b' can be objects of some
UDT that either redefines operators >= or -, or just doesn't have them
defined at all.
Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler
dependent?

Nope. Yep. I don't know. You didn't specify what 'a' and 'b' are.

V
 
T

Tim Prince

Hi!

Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++?
Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler
dependent?


IEEE gradual underflow and infinity implementation is designed to
support this equivalence for floating point arithmetic, in case, for
example, (double)a - b < DBL_MIN. Several important platforms have
dropped support for gradual underflow; it is not assumed by C or C++.
http://grouper.ieee.org/groups/754/faq.html#underflow
Still, compiler optimizers may act as if the equivalence were valid,
even on platforms where it is not.
 
P

P.J. Plauger

Tim Prince said:
(e-mail address removed)-rio.br wrote:
IEEE gradual underflow and infinity implementation is designed to support
this equivalence for floating point arithmetic, in case, for example,
(double)a - b < DBL_MIN. Several important platforms have dropped support
for gradual underflow; it is not assumed by C or C++.
http://grouper.ieee.org/groups/754/faq.html#underflow
Still, compiler optimizers may act as if the equivalence were valid, even
on platforms where it is not.

Well summarized. Note that for fixed-point, the equivalence does
not necessarily hold. If (a - b) overflows, the test result can
easily be wrong. Having said that, I have to confess that the
Whitesmiths compiler generated code like this for the Intel 8080
family. Nobody ever complained.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
G

gnt

Hello,
My name is George Tsalamandris with Chase Winters Executive Search. I
am currently conducting a search for a Application/ Developer Architect
for a major global financial institution.
Below please find a position description for your review. Please
forward to anyone that miught be interested in disscussing this
opportunity. The position is located in Chicago. Look forward to
hearing from you.

George Tsalamandris
(e-mail address removed)
312-421-9080

General Job Responsibilities:

Complete system development lifecycle (analysis, design, development,
test, release, and support) for Global Instrument Reference Data
platform.



Qualifications:



Experience:

5-10 as a developer / application architect in the financial industry;
5+ years with reference data



Technical Skills:

Object Oriented Analysis and Design

C++(including STL) Expertise

Sybase Database Expertise

ODBC Programming Expertise

Experience with the G3 Object Model

Technical Writing, Documentation

Code Generation Experience

Multithreaded Linux Server Development Experience

UML Knowledge

Persistence/Object-to-Relational Mapping Tool Experience

GCC, GNUmake Experience

Linux Operating System Experience

Scripting Language Experience



Leadership / Project Management Skills:

5 Years experience as a technical team lead

Project scheduling and planning with Microsoft Project

Release Management

Defect Management using Clear Quest



Business Knowledge:

Broad product knowledge required including derivative, fixed income,
and equity products. Must have knowledge of the trade lifecycle and
corporate actions.
 
W

Walter Roberson

IEEE gradual underflow and infinity implementation is designed to
support this equivalence for floating point arithmetic, in case, for
example, (double)a - b < DBL_MIN.

Going outside the C standard itself a little (since IEEE floating
point is not part of that standard anyhow): I recall that there
are some unusual rules having to do with comparison operators
and special values, especially NaN. I do not recall the
details, though.

a >= b would have information about two special values available
at the time of the comparison, whereas a - b >= 0 would only
have information about one special value available at the time
of the comparison. Are there perchance particular special values
for which that would make a difference? Or, for example,
in the case of a - b when one of the two values was a
signalling NaN, would the signal be taken due to the arithmetic
operator, but -not- taken in the a >= b case ?

ISTR that the general rule for NaN's is that the result of
any comparison with them is "false" -- one of the standard ways
of detecting them is to detect the case where a >= b and b < a
are -both- false. I do not, though, recall the rules for the
other special values.
 
C

C Wegrzyn

Funny you should mention the Whitesmith compiler. In fact I think most
of us knew about the problem at the "extremes" and took that into
account. I know I dealt with it in writing some software for a nuclear
reactor system that was deployed in Georgia.

I remember as part of a CS degree having classes on interval analysis,
something that came into existence and out almost as fast. There was
only one machine I worked on that used it as part of the processor. It
was for a specialized project to bounce signals off of the surface of
clouds. Of course that and all the numerical analysis classes helped in
dealing with these issues.

I wonder what they teach now a days?

Chuck Wegrzyn
 
K

Keith Thompson

Hello,
Good-bye.

My name is George Tsalamandris with Chase Winters Executive Search. I
am currently conducting a search for a Application/ Developer Architect
for a major global financial institution.

This is not the place to post job offers.
 
R

Randy Howard

C Wegrzyn wrote
(in article said:
I remember as part of a CS degree having classes on interval analysis,
something that came into existence and out almost as fast.
.....

I wonder what they teach now a days?

Chuck Wegrzyn

How to customize websites with music and animation.

*sigh*
 
A

Andrew Koenig

Well summarized. Note that for fixed-point, the equivalence does
not necessarily hold. If (a - b) overflows, the test result can
easily be wrong. Having said that, I have to confess that the
Whitesmiths compiler generated code like this for the Intel 8080
family. Nobody ever complained.

Unfortunately, the equivalence doesn't always hold for floating-point
either.

Consider the case where a is a very large positive number and b is a very
large negative number. Then a >= b will presumably yield true, but a - b
might overflow.

I think I agree with PJP that in the presence of IEEE gradual underflow,
it's not an issue that a and b might be small (or not close to the limits,
anyway) and very nearly equal.
 
D

Dik T. Winter

> Going outside the C standard itself a little (since IEEE floating
> point is not part of that standard anyhow): I recall that there
> are some unusual rules having to do with comparison operators
> and special values, especially NaN. I do not recall the
> details, though.

Yup, all standard comparisons with NaNs yield false. This means, for
instance, that when a and b ara NaNs, a >= b == a <= b == FALSE.
 
D

Dik T. Winter

> Unfortunately, the equivalence doesn't always hold for floating-point
> either.
>
> Consider the case where a is a very large positive number and b is a very
> large negative number. Then a >= b will presumably yield true, but a - b
> might overflow.

In that case a - b yields +inf, and +inf >= 0.
 
A

Anonymous 7843

Yup, all standard comparisons with NaNs yield false. This means, for
instance, that when a and b ara NaNs, a >= b == a <= b == FALSE.

Pedantic nitpick: "!=" is a comparison but yields true for NaNs.
 
A

Anonymous 7843

Pedantic nitpick: "!=" is a comparison but yields true for NaNs.

Me again. I felt the urge for a pretty picture, so here it
is, as seen from an IEEE854-ish machine:

compare -NaN -Inf -1e+10 -1 -1e-10 0 1e-10 1e+10 Inf NaN
------- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-NaN != != != != != != != != != !=
-Inf != == < < < < < < < !=
-1e+10 != > == < < < < < < !=
-1 != > > == < < < < < !=
-1e-10 != > > > == < < < < !=
0 != > > > > == < < < !=
1e-10 != > > > > > == < < !=
1e+10 != > > > > > > == < !=
Inf != > > > > > > > == !=
NaN != != != != != != != != != !=

-----------------quickie code-------------------

#include <stdio.h>

int main(void)
{
double inf, nan;
double sample[] = {0, 0, -1E+10, -1, -1E-10, 0, +1E-10, +1E+10, 0, 0};
size_t i, j, len;

sscanf("Inf", "%lf", &inf);
sscanf("NaN", "%lf", &nan);
sample[0] = -nan;
sample[1] = -inf;
sample[8] = inf;
sample[9] = nan;

len = sizeof(sample) / sizeof(sample[0]);

printf("compare ");
for (j = 0; j < len; j++)
printf(" %-6g", sample[j]);
printf("\n");

printf("------- ");
for (j = 0; j < len; j++)
printf(" -----");
printf("\n");

for (i = 0; i < len; i++)
{
printf("%-6g ", sample);
for (j = 0; j < len; j++)
{
if (sample == sample[j]) printf(" == ");
else if (sample > sample[j]) printf(" > ");
else if (sample < sample[j]) printf(" < ");
else if (sample >= sample[j]) printf(" >= ");
else if (sample <= sample[j]) printf(" <= ");
else if (sample != sample[j]) printf(" != ");
}
printf("\n");
}

return 0;
}
 

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
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top