TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MSTG007 on January 18, 2017, 11:56:04 AM

Title: Having some rounding issues
Post by: MSTG007 on January 18, 2017, 11:56:04 AM
I do not know if I did something awhile ago to get this not work... (I am good at that). But I am having some rounding issues. The routine is suppose to override the dim text from <> to (X SPACES @ X' = <>').

For example if the dim is 63'. The label now shows it to be 6 Spaces @ 9'. 6x9=54.

But in the same drawing on a different dimension. The dim is 162'. The Label shows 18 Spaces @ 9' = 162'. Which is right.

The Routine is within a macro.

^C^C-dimstyle;V;L80;DIMZIN;8;-dimstyle;apply;all;;(load "SPCT");SPCT;

Any ideas?

Code: [Select]
(defun C:SPCT
  (/ ename obj dim div total fract)
  (setq ename (car (entsel "\nSelect dimension: ")))
  (setq obj (vlax-ename->vla-object ename))
  (setq dim (vla-get-measurement obj))
  (if (not def)
    (setq def 1))
  (setq div (getdist (strcat "\nDivide into <" (rtos def) ">: ")))
  (if (not div)
    (setq div def)
    (setq def div))

  (setq total (/ dim div))
  (setq fract (- total (fix total)))

  (vla-put-textoverride
    obj
    (strcat (itoa (fix total))
    " SPACES @ "
    (rtos div)
             "'"
             " = <>"
       ))
  (princ))

Thanks for the help!
Title: Re: Having some rounding issues
Post by: ronjonp on January 18, 2017, 12:22:02 PM
Are you sure your dimension is exactly 63'? (setq total (fix (/ 62.999999 9))) = 6
Title: Re: Having some rounding issues
Post by: Lee Mac on January 18, 2017, 12:44:52 PM
I would suggest changing all occurrences of:
Code: [Select]
(fix total)to:
Code: [Select]
(fix (+ 1e-8 total))
Title: Re: Having some rounding issues
Post by: MSTG007 on January 18, 2017, 01:00:59 PM
Ron and Lee,
Thanks for the pointers. That's what it was missing. Thank you guys again!