Hi all,
I have this little lisp that is part of a larger lisp I am working on.
Basically it gets the viewport scale and displays it either 1/16"=1'-0" or 1"=20' depending on the scale factor.
It keeps glitching out on 360 (1"=30') and 720 (1"=60')
On the 360 it displays 1/32, 1/32 is 384?
I've tried using cond statement and if.
I'm hoping it's something obvious I'm missing.
See attached lisp
That may be one of those things resulting from calculation functions, in which the result could be something like 360.000000000000427, in which case the (=) function, which requires precise equality, will not recognize it as equal to 360. If that's the cause, the solution is to use (equal) with a small fuzz factor, rather than (=). And since the scale factors are all multiples of 120, you don't need to list all of them separately. Try replacing your long (or) function listing all those factors with this:
(if (equal (rem vp_scale 120.0) 0.0 1e-4)
(progn ….
Well,
I love the simplified code.. but..
360 still displays 1/32
and 720 displays 1/64...
????
Well it looks like 720 does actually calculate to 719.9999999999
So i tried to fool it with (and (> vp_scale 719.0)(< vp_scale 721.0)) ... no luck
360 calculates out to 360.000000000 ???
I got this to work... seems a little hap-hazard though..
(if (or (= vp_scale 120.0)(= vp_scale 240.0)(and (> vp_scale 359.75)(< vp_scale 360.25))(= vp_scale 480.0)(= vp_scale 600.0)(and (> vp_scale 719.75)(< vp_scale 720.0))(= vp_scale
840.0)(= vp_scale 960.0)(= vp_scale 1200.0))
Here's another way to skin the cat 🙂
(if (vl-some '(lambda (x) (equal x vp_scale 1e-4)) '(120 240 360 480 600 720 840 960 1200)) (progn (setq vp_scale1 (strcat "1\" = " (rtos (/ vp_scale 12) 2 0) "\'")) (princ vp_scale1)) (progn (setq vp_scale1 (strcat (rtos (/ 1 (* vp_scale (/ 1.0 12))) 4) " = 1\'-0\"")) (princ vp_scale1) ) )
@DC-MWA wrote:I'll try it.
Thanks!
Glad to help :). You could also use Kent's recommendation and test for two possible values.
(setq vp_scale 719.9999999999) (or (equal (rem vp_scale 120.) 0 1e-4) (equal (rem vp_scale 120.) 120 1e-4)) ;; Example below what the two values return with REM (alert (vl-princ-to-string (rem 719.9999999999 120.))) (alert (vl-princ-to-string (rem 720.0000000001 120.)))
Can't find what you're looking for? Ask the community or share your knowledge.