problems with (equal ...)

problems with (equal ...)

Anonymous
Not applicable
585 Views
11 Replies
Message 1 of 12

problems with (equal ...)

Anonymous
Not applicable
Why do the following lines of code return what they do? I have had so
many problems understanding the true definition of the (equal) function,
it's getting ridiculous 🙂

Command: (equal 1.01 1.02 0.01)
nil
Command: (equal 1.001 1.002 0.001)
nil
Command: (equal 1.1 1.2 0.1)
T
Command: (equal 1.0 2.0 1.0)
nil

Thanks for any and all help!
Craig Black
0 Likes
586 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
It has to do with the last digit. (equal 1.02 1.01 .01) returns nil but
(equal 1.01 1.099 0.1) returns T.

"Craig Black" wrote in message
news:[email protected]...
> Why do the following lines of code return what they do? I have had so
> many problems understanding the true definition of the (equal) function,
> it's getting ridiculous 🙂
>
> Command: (equal 1.01 1.02 0.01)
> nil
> Command: (equal 1.001 1.002 0.001)
> nil
> Command: (equal 1.1 1.2 0.1)
> T
> Command: (equal 1.0 2.0 1.0)
> nil
>
> Thanks for any and all help!
> Craig Black
>
0 Likes
Message 3 of 12

Anonymous
Not applicable
Frank,

What he means is that 1.01 is obviously only 0.01 away from being equal to
1.02. So (equal expr1 expr2 [fuzz]) _should_ return T for (equal 1.01 1.02
0.01)! In fact, I was surprised when I saw his post, and verified the
problem with R14 & AC2K. This could hurt!

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)
(remove the "not." in my address for direct e-mail)

Frank Oquendo wrote in message
news:[email protected]...
> It has to do with the last digit. (equal 1.02 1.01 .01) returns nil but
> (equal 1.01 1.099 0.1) returns T.
>
> "Craig Black" wrote in message
> news:[email protected]...
> > Why do the following lines of code return what they do? I have had so
> > many problems understanding the true definition of the (equal) function,
> > it's getting ridiculous 🙂
> >
> > Command: (equal 1.01 1.02 0.01)
> > nil
> > Command: (equal 1.001 1.002 0.001)
> > nil
> > Command: (equal 1.1 1.2 0.1)
> > T
> > Command: (equal 1.0 2.0 1.0)
> > nil
> >
> > Thanks for any and all help!
> > Craig Black
> >
>
>
0 Likes
Message 4 of 12

Anonymous
Not applicable
"R. Robert Bell" wrote:
>
> What he means is that 1.01 is obviously only 0.01 away from being equal to
> 1.02. So (equal expr1 expr2 [fuzz]) _should_ return T for (equal 1.01 1.02
> 0.01)! In fact, I was surprised when I saw his post, and verified the
> problem with R14 & AC2K. This could hurt!

I'm not sure where you came to the conclusion that (equal)
should return T in the cited cases, but that is simply not
the case at all.

The purpose of (equal ) is to provide a
way to compare two numbers to a specified precision, and the
reason for it is because (eq) and (=) are both subject to
floating point roundoff, meaning that even though two values
appear to be equal, they may not be treated that way at the
precision that (eq) and (=) use (the underlying C++ runtime
library is what ultimately determines this, and two different
libraries, or even different releases of the same library
could easily yield different results given the same values).

However, when using this form of (equal), and the difference
between the two numbers appears to be equal to the ,
you are once again faced with the very same problem that
(equal ) was intended to solve: floating
point roundoff.

IOW, the underlying implementation of (equal) when used to
compare two numbers with fuzz, is basically this:

bool fuzzy-equal(double a, double b, double fuzz)
{
return abs(a - b) < fuzz;
}

Hence, in a case where the difference between the two
numbers appears to be equal to the fuzz, the success or
failure of (equal) is entirely dependent on how the C
runtime library's "<" (less-than) operator behaves given
these two operands. And, of course, that's essentially the
very same problem you have when using the (=) or (eq)
functions to compare two doubles (IOW, it's pot luck).

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* [email protected] */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 5 of 12

Anonymous
Not applicable
Nicely stated. I knew what the problem was but I couldn't find a way to
explain it. Thank you.

"Tony Tanzillo" wrote in message
news:[email protected]...
> "R. Robert Bell" wrote:
> >
> > What he means is that 1.01 is obviously only 0.01 away from being equal
to
> > 1.02. So (equal expr1 expr2 [fuzz]) _should_ return T for (equal 1.01
1.02
> > 0.01)! In fact, I was surprised when I saw his post, and verified the
> > problem with R14 & AC2K. This could hurt!
>
> I'm not sure where you came to the conclusion that (equal)
> should return T in the cited cases, but that is simply not
> the case at all.
>
> The purpose of (equal ) is to provide a
> way to compare two numbers to a specified precision, and the
> reason for it is because (eq) and (=) are both subject to
> floating point roundoff, meaning that even though two values
> appear to be equal, they may not be treated that way at the
> precision that (eq) and (=) use (the underlying C++ runtime
> library is what ultimately determines this, and two different
> libraries, or even different releases of the same library
> could easily yield different results given the same values).
>
> However, when using this form of (equal), and the difference
> between the two numbers appears to be equal to the ,
> you are once again faced with the very same problem that
> (equal ) was intended to solve: floating
> point roundoff.
>
> IOW, the underlying implementation of (equal) when used to
> compare two numbers with fuzz, is basically this:
>
> bool fuzzy-equal(double a, double b, double fuzz)
> {
> return abs(a - b) < fuzz;
> }
>
> Hence, in a case where the difference between the two
> numbers appears to be equal to the fuzz, the success or
> failure of (equal) is entirely dependent on how the C
> runtime library's "<" (less-than) operator behaves given
> these two operands. And, of course, that's essentially the
> very same problem you have when using the (=) or (eq)
> functions to compare two doubles (IOW, it's pot luck).
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* [email protected] */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 6 of 12

Anonymous
Not applicable
I know that roundoff is the problem. I was not aware that roundoff would
rear its ugly head at such a large "implied" precision. Guess that's why I
always used larger fuzz factors than I thought I needed! Thanks for the
explanation, Tony.

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)
(remove the "not." in my address for direct e-mail)

Tony Tanzillo wrote in message
news:[email protected]...
> "R. Robert Bell" wrote:
> >
> > What he means is that 1.01 is obviously only 0.01 away from being equal
to
> > 1.02. So (equal expr1 expr2 [fuzz]) _should_ return T for (equal 1.01
1.02
> > 0.01)! In fact, I was surprised when I saw his post, and verified the
> > problem with R14 & AC2K. This could hurt!
>
> I'm not sure where you came to the conclusion that (equal)
> should return T in the cited cases, but that is simply not
> the case at all.
>
> The purpose of (equal ) is to provide a
> way to compare two numbers to a specified precision, and the
> reason for it is because (eq) and (=) are both subject to
> floating point roundoff, meaning that even though two values
> appear to be equal, they may not be treated that way at the
> precision that (eq) and (=) use (the underlying C++ runtime
> library is what ultimately determines this, and two different
> libraries, or even different releases of the same library
> could easily yield different results given the same values).
>
> However, when using this form of (equal), and the difference
> between the two numbers appears to be equal to the ,
> you are once again faced with the very same problem that
> (equal ) was intended to solve: floating
> point roundoff.
>
> IOW, the underlying implementation of (equal) when used to
> compare two numbers with fuzz, is basically this:
>
> bool fuzzy-equal(double a, double b, double fuzz)
> {
> return abs(a - b) < fuzz;
> }
>
> Hence, in a case where the difference between the two
> numbers appears to be equal to the fuzz, the success or
> failure of (equal) is entirely dependent on how the C
> runtime library's "<" (less-than) operator behaves given
> these two operands. And, of course, that's essentially the
> very same problem you have when using the (=) or (eq)
> functions to compare two doubles (IOW, it's pot luck).
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* [email protected] */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 7 of 12

Anonymous
Not applicable
Thank you all for letting me know that it isn't reliable!!
Would it be safe to say that the definition of the equal function would be:

Returns T if the difference between the first 2 real arguments is less than the
(not less than or equal to)?

Cb

Tony Tanzillo wrote:

> "R. Robert Bell" wrote:
> >
> > What he means is that 1.01 is obviously only 0.01 away from being equal to
> > 1.02. So (equal expr1 expr2 [fuzz]) _should_ return T for (equal 1.01 1.02
> > 0.01)! In fact, I was surprised when I saw his post, and verified the
> > problem with R14 & AC2K. This could hurt!
>
> I'm not sure where you came to the conclusion that (equal)
> should return T in the cited cases, but that is simply not
> the case at all.
>
> The purpose of (equal ) is to provide a
> way to compare two numbers to a specified precision, and the
> reason for it is because (eq) and (=) are both subject to
> floating point roundoff, meaning that even though two values
> appear to be equal, they may not be treated that way at the
> precision that (eq) and (=) use (the underlying C++ runtime
> library is what ultimately determines this, and two different
> libraries, or even different releases of the same library
> could easily yield different results given the same values).
>
> However, when using this form of (equal), and the difference
> between the two numbers appears to be equal to the ,
> you are once again faced with the very same problem that
> (equal ) was intended to solve: floating
> point roundoff.
>
> IOW, the underlying implementation of (equal) when used to
> compare two numbers with fuzz, is basically this:
>
> bool fuzzy-equal(double a, double b, double fuzz)
> {
> return abs(a - b) < fuzz;
> }
>
> Hence, in a case where the difference between the two
> numbers appears to be equal to the fuzz, the success or
> failure of (equal) is entirely dependent on how the C
> runtime library's "<" (less-than) operator behaves given
> these two operands. And, of course, that's essentially the
> very same problem you have when using the (=) or (eq)
> functions to compare two doubles (IOW, it's pot luck).
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* [email protected] */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 8 of 12

Anonymous
Not applicable
Craig Black wrote:
>
> Thank you all for letting me know that it isn't reliable!!
> Would it be safe to say that the definition of the equal function would be:
>
> Returns T if the difference between the first 2 real arguments is less than the
> (not less than or equal to)?

Yes, except that you can substitue "less than or equal
to" in place of "less than". Of course, all that says is
that the definition is dependent on the floating point
roundoff in the "less-than-or-equal-to" comparison that
is ultimately performed by the VC++ runtime library code.

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* [email protected] */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 9 of 12

Anonymous
Not applicable
Yup!

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)
(remove the "not." in my address for direct e-mail)

Craig Black wrote in message
news:[email protected]...
> Thank you all for letting me know that it isn't reliable!!
> Would it be safe to say that the definition of the equal function would
be:
>
> Returns T if the difference between the first 2 real arguments is less
than the
> (not less than or equal to)?
>
> Cb
>
> Tony Tanzillo wrote:
>
> > "R. Robert Bell" wrote:
> > >
> > > What he means is that 1.01 is obviously only 0.01 away from being
equal to
> > > 1.02. So (equal expr1 expr2 [fuzz]) _should_ return T for (equal 1.01
1.02
> > > 0.01)! In fact, I was surprised when I saw his post, and verified the
> > > problem with R14 & AC2K. This could hurt!
> >
> > I'm not sure where you came to the conclusion that (equal)
> > should return T in the cited cases, but that is simply not
> > the case at all.
> >
> > The purpose of (equal ) is to provide a
> > way to compare two numbers to a specified precision, and the
> > reason for it is because (eq) and (=) are both subject to
> > floating point roundoff, meaning that even though two values
> > appear to be equal, they may not be treated that way at the
> > precision that (eq) and (=) use (the underlying C++ runtime
> > library is what ultimately determines this, and two different
> > libraries, or even different releases of the same library
> > could easily yield different results given the same values).
> >
> > However, when using this form of (equal), and the difference
> > between the two numbers appears to be equal to the ,
> > you are once again faced with the very same problem that
> > (equal ) was intended to solve: floating
> > point roundoff.
> >
> > IOW, the underlying implementation of (equal) when used to
> > compare two numbers with fuzz, is basically this:
> >
> > bool fuzzy-equal(double a, double b, double fuzz)
> > {
> > return abs(a - b) < fuzz;
> > }
> >
> > Hence, in a case where the difference between the two
> > numbers appears to be equal to the fuzz, the success or
> > failure of (equal) is entirely dependent on how the C
> > runtime library's "<" (less-than) operator behaves given
> > these two operands. And, of course, that's essentially the
> > very same problem you have when using the (=) or (eq)
> > functions to compare two doubles (IOW, it's pot luck).
> >
> > --
> > /*********************************************************/
> > /* Tony Tanzillo Design Automation Consulting */
> > /* Programming & Customization for AutoCAD & Compatibles */
> > /* ----------------------------------------------------- */
> > /* [email protected] */
> > /* http://ourworld.compuserve.com/homepages/tonyt */
> > /*********************************************************/
>
0 Likes
Message 10 of 12

Anonymous
Not applicable
No, only to say in addition that the results
are unreliable when numbers difference is close
to the fuzz factor.

Consider this:

Command: (equal 1.123456 1.123457 0.000001) T
Command: (equal 1.1 1.2 0.1) T
Command: (equal 1.01 1.02 0.01) nil

IOW, the test is unreliable when difference
equals the fuzz factor, like Tony explained
in his first response here. You're supposed
to compare the numbers closer then fuzz, but
when "on the edge", the results are unpredictable.

The documentation is misleading in that it should've
point this out.

On Wed, 02 Feb 2000 15:19:05 -0600, Craig Black
wrote:

>Would it be safe to say that the definition of the equal function would be:
>
>Returns T if the difference between the first 2 real arguments is less than the
> (not less than or equal to)?
0 Likes
Message 11 of 12

Anonymous
Not applicable
What Vladimir said. Note that numbers are stored in the computer in binary form.
There is no exact binary equivalent for 0.01 base 10.

Jrf
Member of the Autodesk Discussion Forum Moderator Program

In article <[email protected]>, Craig Black wrote:
> Thank you all for letting me know that it isn't reliable!!
> Would it be safe to say that the definition of the equal function would be:
>
> Returns T if the difference between the first 2 real arguments is less than the
> (not less than or equal to)?
0 Likes
Message 12 of 12

Anonymous
Not applicable
> Craig Black wrote:
> >
> > Thank you all for letting me know that it isn't reliable!!
> > Would it be safe to say that the definition of the equal function would be:
> >
> > Returns T if the difference between the first 2 real arguments is less than the
> > (not less than or equal to)?
>
> Yes, except that you can substitue "less than or equal
> to" in place of "less than". Of course, all that says is
> that the definition is dependent on the floating point
> roundoff in the "less-than-or-equal-to" comparison that
> is ultimately performed by the VC++ runtime library code.
>

I would say that you *can't* say "less than or equal to" if 'equal to' does not
always work... It seems that "less than" always works...

Thanks,
Cb
0 Likes