TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hunterxyz on May 18, 2008, 09:14:37 AM
-
(SETQ AA 10)
(SETQ BB 0.538516)
(SETQ CC (*(COS 1.9513026954188)BB))
(SETQ DD (ABS (* CC AA)))
_$ 2.0
(SETQ EE (FIX DD))
_$ 1
Asks fellow masters to explain thanks ~
-
Hi,
May be a difference between the 5 decimal rounded displayed value (2.0) and the 16 decimal rounded value used in calculus (1.999998172232795)
Try (rtos DD 2 16)
-
(SETQ AA 10)
(SETQ BB 0.538516)
(SETQ CC (*(COS 1.9513026954188)BB))
(SETQ DD (ABS (* CC AA)))
_$ 2.0
(setq dd$ (rtos dd 2 15))
"1.999998172232795"
(setq dd# (atof dd$))
;;;_$ 2.0
(setq dd& (fix dd#))
;;;_$ 1
(SETQ EE (FIX DD))
;;;_$ 1
-
_$ 1.99999
1.99999
_$ 1.999999
2.0
visual lisp console uses some kind of rtos to print out the results. don't trust what you see :)
-
How to let SET EE = 2
-
(distof(rtos dd 2 0))
-
Hi,
Instead of fix function, you can use a rounding function, there're many around here, you can see this thread (http://www.theswamp.org/index.php?topic=18695.0).
Here's mine (last release)
(defun round (num prec)
(if (zerop (setq prec (abs prec)))
num
(if (minusp num)
(* prec (fix (- (/ num prec) 0.5)))
(* prec (fix (+ (/ num prec) 0.5)))
)
)
)
Using:
(round pi 0.01) -> 3.14
(round pi 1e-5) -> 3.14159
(round 5456.50 1) -> 5457
(round 5456.50 100.0) -> 5500.0
Note that if prec argument is an interger, the result will be an integer too.
-
Here is mine and another thread.
http://www.theswamp.org/index.php?topic=8511.msg109002#msg109002
-
another one using vb data types
(vl-load-com)
;;;range (Signed) of vlax-vbLong is −2147483648 to 2147483647
;;;uses 'Round-to-even' method
(defun Int (InReal)
(vlax-variant-value (vlax-make-variant InReal vlax-vbLong))
)
_$ (Int 3.5)
4
_$ (Int 4.5)
4
-
Hey Vlad, if 3.5 rounds to 4, should 4.5 round to 5 and maybe 4.49 round to 4?
-
no way, Daron :wink:
http://en.wikipedia.org/wiki/Rounding#Round-to-even_method (http://en.wikipedia.org/wiki/Rounding#Round-to-even_method)
-
Wow...you learn something new everyday. I just learnt mine. Does that mean I get to go home now?
-
Great link VovKa 8-)