TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Water Bear on February 03, 2004, 05:46:05 PM
-
I set the same point to different variables, then compared them in an IF statement...it says they are not equal! (note the watch) :shock:
(http://theswamp.org/lilly.pond/Water%20Bear/Resize-Wizard-1.jpg)
This IF is followed by a conditional which gives me the incorrect option because this IF fails. what gives?
-
Would replacing = with eq or equal help?
-
You can see in the watch window that the two values "look" similar, but in actuality are not if they were expanded to 15 significant digits. The = function tests numerical equality and they _have+ to be exactly equal to get T.
Using (equal num1 num2 fuzz) would be what you want to do here. Set fuzz to a very small number (0.000001) and try it.
-
yep..that did the trick! thanx
-
When is equal not equal?
They are not the same (= eq equal)
I find (equal) to be the most forgiving and if that is not enough
you can use the fuzz factor.
Avoid using = on points.
Equal
Determines whether two expressions are equal
(equal expr1 expr2 [fuzz])
A real number defining the maximum amount by which expr1 and expr2 can differ and still be considered equal.
When comparing two real numbers (or two lists of real numbers, as in points), the two identical numbers can differ slightly if different methods are used to calculate them. You can specify a fuzz amount to compensate for the difference that may result from the different methods of calculation.
=
(equal to) Compares arguments for numerical equality
eq
Determines whether two expressions are identical
-
By three witnesses, the truth shall be known.
-
Avoid using = on points
Exactly.
"=" is for numbers, strings and symbol names, and that's it.
(setq a '("a" "b" "c" 1 2.0))
("a" "b" "c" 1 2.0)
(setq b '("a" "b" "c" 1 2.0))
("a" "b" "c" 1 2.0)
Are these lists equal to each other? Certainly, they seem to be but ...
(= a b) -> nil
Each item may be tested T with "=",
(mapcar '= a b) -> (T T T T T)
because it is testing what it's designed to do: numbers and strings. But comparing lists with it is no good.
EQ will also "fail" the test:
(eq a b) -> nil
EQ is comparing bindings, which means that a and b need to be bound to the same thing in order to be found equal. In this case, each list was build separately and has nothing to do with each other internally. Binding variables to the same object will make them equal in the eyes of EQ:
(setq c a)
("a" "b" "c" 1 2.0)
(eq a c) -> T
Only EQUAL compares lists by their literal contents:
(equal a b) -> T
By the way, using "=" to compare the variables after they have been bound to the same list will return T, which can cause one to think that EQ and "=" are alike:
(= a c) -> T
But try using them on, say, entity names:
(setq e1 (entlast))
<Entity name: 7ef5ce90>
(setq e2 (entlast))
<Entity name: 7ef5ce90>
e1 and e2 will be bound to the same physical object in memory, so EQ returns T:
(eq e1 e2) -> T
But "=" returns nil even though the objects are identical. It is not designed to handle those data types:
(= e1 e2) -> nil