|
![]() |
Different data types and different sources of calculations yield different levels of accuracy.
Integers are completely accurate within their allowed range (-2,147,483,648 through +2,147,483,647).
Floating point real numbers are accurate to between 14 and 17 significant digits.
Trigonometric functions are somewhat less accurate than reals. This can cause some misleading results from the tests described below. Numbers which theoretically should be equal, may not be stored accurately enough for the tests to give the result you expect. For example, the sine of 30° is 0.5. But if we ask AutoLISP to compare the sine of 30° with 0.5, it says they are not equal! Try it out (since the trig functions work with radians, you need to substitute one-sixth p for 30°): (= 0.5 (sin (/ pi 6))) returns nil !
This highlights the importance of (1) knowing which data type your program is working with, and (2) when needed, using the equal function, explained below, which allows for a margin of error.
(= 7 7.0) returns T
After (setq a 7 b 6.9)
(= a b) returns nil
(= 4.8 4.80 4.8000) returns T
(= "hello" "hello") returns T
(= "hello" "Hello") returns nil
(= 10 "ten") returns nil
After (setq L1 '(j k) L2 '(j k) L3 L1)
(eq L1 L2) returns nil
(eq L1 L3) returns T
After (setq L1 '(j k) L2 '(j k) L3 '(j
q))
(equal L1 L2) returns T
(equal L1 L3) returns nil
Many times there will be two real numbers which you would consider equal for all practical purposes, but which actually differ very slightly. (See the "NOTE REGARDING ACCURACY" at the top of this page.) This creates a problem because the equal function considers these two numbers unequal. However, the equal function will accept a third argument which indicates the allowable margin of error.
(equal 3.007 3.008) returns nil
(equal 3.007 3.008 0.001) returns T because the margin of error
is 0.001
(equal 95 188 100) returns T because the margin of error is 100
After (setq a 5.00006 b 5.00008)
(equal a b) returns nil
(equal a b 0.00002) returns T
This margin of error can also be applied to the distance between points.
After (setq p1 (list 1.00000000 1.00000000) p2 (list 1.00000001 0.99999999)
(equal p1 p2) returns nil
(equal p1 p2 0.00000001) returns nil
(equal p1 p2 0.00000002) returns T
(/= 7 6.9) returns T
(/= 7 7.0000) returns nil
(/= "high" "low") returns T
(/= "High" "high") returns T
(< 5 10) returns T
(< 10 9.999999999) returns nil
(< -5 1) returns T
After (setq a 2 b 4 c 6 d 8)
(< a b c d) returns T
(< a b c c) returns nil
(<= 5 10) returns T
(<= 2 4 6 6) returns T
(<= 11 -12 13) returns nil
(> 5 10) returns nil
(> 10.0 10.0) returns nil
After (setq a 18 b 17 c 16 d 15)
(> a b c d) returns T
(> a b b c) returns nil
(>= 10 5) returns T
(>= 18 17 17 16) returns T
(>= -99 2) returns nil
(minusp 0) returns nil
(minusp -3.3) returns T
(minusp 4) returns nil
(numberp 2) returns T
(numberp 2.193) returns T
(numberp "ten") returns nil
(zerop 0) returns T
(zerop 0.000) returns T
(zerop 0.00000000000000001) returns nil