Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

When is equality NOT equality?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
NashGussetts
528 Views, 8 Replies

When is equality NOT equality?

I have the following lines in a lager routine I'm debugging:

 

      (If (<= (/ mezzarea suitearea) 0.1)         
         .... do stuff

 

mezzarea is EXACTLY one tenth of suitearea and they are both reals, so I'm asking is 0.1 <= 0.1, yet the line returns nil no matter what.

 

Doubt me? See attached jpeg from the console window.

 

 

8 REPLIES 8
Message 2 of 9
Kent1Cooper
in reply to: NashGussetts


@NashGussetts wrote:

I have the following lines in a lager routine I'm debugging:

 

      (If (<= (/ mezzarea suitearea) 0.1)         
         .... do stuff

 

mezzarea is EXACTLY one tenth of suitearea and they are both reals, so I'm asking is 0.1 <= 0.1, yet the line returns nil no matter what.

.... 


It's probably in some far-in decimal place beyond what your display rounds things to.  Try forcing it to show you lots of decimal places by comparing:

 

(rtos mezzarea 2 16)

 

to

 

(rtos (/ suitearea 10) 2 16)

 

to see whether they're really as equal as you think.  If not, you can overcome that kind of calculation fuzziness by checking for equality with a tiny fuzz factor:

 

(If

  (or

    (< (/ mezzarea suitearea) 0.1)

    (equal (/ mezzarea suitearea) 0.1 1e-6)

  ); or

  (...then do stuff...)

); if

 

You can use a different number of decimal places [replace the 6 above with something else].

Kent Cooper, AIA
Message 3 of 9
NashGussetts
in reply to: NashGussetts

Thanks, I had a feeling it was a fuzz problem but when I checked the help on <= it was silent on how/whether fuzz was a 'factor'

 

Writing this stuff is hard enough without the machine telling you 0.1/=0.1

Message 4 of 9
Kent1Cooper
in reply to: NashGussetts


@NashGussetts wrote:

Thanks, I had a feeling it was a fuzz problem but when I checked the help on <= it was silent on how/whether fuzz was a 'factor'

 

Writing this stuff is hard enough without the machine telling you 0.1/=0.1


It may be that among AutoLISP functions, only (equal) can account for fuzz -- I'm not sure that's the only one.  [PEDIT in the Multiple/Join mode has an option for it.]  Of couse, it's not really telling you that 0.1 doesn't equal 0.1, but that one number that displays as 0.1 may not be the same, all the way down to the precision with which it works in the background, as another number that displays as 0.1.  If you set two variables to 0.1 explicitly, they will be equal -- the problem comes in when a number is the result of some calculation, such as of the area of, say, a Polyline perimeter, especially if it doesn't have nice clean round-number-value vertex coordinates and orthogonal edges.

Kent Cooper, AIA
Message 5 of 9
Lee_Mac
in reply to: Kent1Cooper

Kent1Cooper wrote:

(If

  (or

    (< (/ mezzarea suitearea) 0.1)

    (equal (/ mezzarea suitearea) 0.1 1e-6)

  ); or

  (...then do stuff...)

); if

 

You could also simply add the tolerance to the upper bound for the <= expression, e.g.:

 

(if (<= (/ mezzarea suitearea) (+ 0.1 1e-6))
    ... do stuff ...
)
Message 6 of 9
BeKirra
in reply to: NashGussetts

Yes, AutoLispers have to deal with such issues.

 

This may be off the topic but FYI:

Supposedly Tan45(degrees) = 1.0 exactly but

(rtos (sin (/ pi 4)) 2 16) = "0.7071067811865475", while
(rtos (cos (/ pi 4)) 2 16) = "0.7071067811865476".

As the result, TAN45(degrees) = "0.9999999999999998".

Note: TAN(angle) = (SIN(angle)) / (COS(angle))

 

And it may explain why AutoLisp doesn't offer a "TAN" function.

There are something behind the AutoLisp and may relate to the processor.

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
Message 7 of 9
Kent1Cooper
in reply to: BeKirra


@BeKirra wrote:

Yes, AutoLispers have to deal with such issues.

 

This may be off the topic but FYI:

Supposedly Tan45(degrees) = 1.0 exactly but

(rtos (sin (/ pi 4)) 2 16) = "0.7071067811865475", while
(rtos (cos (/ pi 4)) 2 16) = "0.7071067811865476".

As the result, TAN45(degrees) = "0.9999999999999998".

Note: TAN(angle) = (SIN(angle)) / (COS(angle))

 

And it may explain why AutoLisp doesn't offer a "TAN" function.

There are something behind the AutoLisp and may relate to the processor.



@BeKirra wrote:

....

Supposedly Tan45(degrees) = 1.0 exactly but

(rtos (sin (/ pi 4)) 2 16) = "0.7071067811865475", while
(rtos (cos (/ pi 4)) 2 16) = "0.7071067811865476".

As the result, TAN45(degrees) = "0.9999999999999998".

....


That is probably not just a processing-precision issue, but as much a result of the fact that it works in radians rather than degrees.  45 is an exact number of degrees, but (/ pi 4) is not an exact number of radians, since pi is irrational.  The system can work with only a certain number of decimal places, so the value is necessarily rounded off.

Kent Cooper, AIA
Message 8 of 9
NashGussetts
in reply to: Kent1Cooper

Yeah, I remember this. Would be nice if VLISP gave you more ways to acount for the fuzz factor and if the IDE would tell you the actual value of a real.

 

Whose side are they on? ; )

Message 9 of 9
Kent1Cooper
in reply to: NashGussetts


@NashGussetts wrote:

Yeah, I remember this. Would be nice if VLISP gave you more ways to acount for the fuzz factor and if the IDE would tell you the actual value of a real.

 

Whose side are they on? ; )


The side of reality, I'm afraid.  After all, if you were asked to tell the "actual value" of, for instance, (/ pi 4), what would you say?  Pi has been calculated out to several million [or is it billion?] decimal places....

 

[And sorry about the double quoting in my previous message -- apparently the quote button registered twice, and the first one scrolled up where I didn't see it.  I only meant to include the edited-down second quote.]

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost