TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hunterxyz on May 18, 2008, 09:14:37 AM

Title: WHY "FIX" FUNCTION ERROR ?
Post 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 ~
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: gile on May 18, 2008, 09:41:40 AM
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)
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: DEVITG on May 18, 2008, 01:11:35 PM
(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
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: VovKa on May 18, 2008, 01:38:53 PM
_$ 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 :)
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: hunterxyz on May 18, 2008, 07:30:30 PM
How to let SET EE = 2
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: Keith™ on May 18, 2008, 08:58:50 PM
Code: [Select]
(distof(rtos dd 2 0))
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: gile on May 19, 2008, 04:07:26 AM
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)

Code: [Select]
(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.
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: CAB on May 19, 2008, 08:45:02 AM
Here is mine and another thread.
http://www.theswamp.org/index.php?topic=8511.msg109002#msg109002
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: VovKa on May 19, 2008, 01:31:41 PM
another one using vb data types
Code: [Select]
(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
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: daron on May 19, 2008, 02:20:30 PM
Hey Vlad, if 3.5 rounds to 4, should 4.5 round to 5 and maybe 4.49 round to 4?
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: VovKa on May 19, 2008, 05:15:39 PM
no way, Daron  :wink:
http://en.wikipedia.org/wiki/Rounding#Round-to-even_method (http://en.wikipedia.org/wiki/Rounding#Round-to-even_method)
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: Columbia on May 20, 2008, 08:05:06 AM
Wow...you learn something new everyday.  I just learnt mine.  Does that mean I get to go home now?
Title: Re: WHY "FIX" FUNCTION ERROR ?
Post by: CAB on May 20, 2008, 08:36:01 AM
Great link VovKa  8-)