TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Red Nova on November 28, 2017, 11:36:41 AM

Title: "less than or equal to" and similar functions not returning proper value
Post by: Red Nova on November 28, 2017, 11:36:41 AM
Hi gens,

I faced this situation a few times. Sometimes if I get some length from the drawing and then try to work with it in lisp some functions do not return proper values.
For example Length of a segment is 12.5 (taken from the drawing), when I compare it to a real number
(<= 12.5 SEGMENTLENGTH) might return nil.
Well as far as I know 12.5 is equal to 12.5 :thinking:, so I assume the issue has to do with precision.
Maybe in reality it considers for example 12.5000001, but the Watch Window will only show that SEGMENTLENGTH is 12.5.
Questions:
1. How can I see in the Watch Window the exact value instead of 12.5 in this case?
2. Is the only solution rounding the variable before I compare it?

Thanks
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: ronjonp on November 28, 2017, 11:42:34 AM
Use equal with fuzz value.
Code - Auto/Visual Lisp: [Select]
  1. (equal 12.5 12.500000001 1e-8)
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: Grrr1337 on November 28, 2017, 11:56:18 AM
Or maybe work with a given precision:
Code - Auto/Visual Lisp: [Select]
  1. (read (rtos 12.500000001 2 5))
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: MP on November 28, 2017, 12:26:25 PM
Code: [Select]
(defun _Fix ( n prec )
    (/ (fix (+ (if (minusp n) -0.5 0.5) (* n (setq prec (expt 10.0 prec)))))
       prec
    )
)

(_Fix pi 3) >> 3.142
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: Red Nova on November 28, 2017, 12:52:41 PM
Thank you guys.

I already knew of methods described by Grrr1337 and MP while Grrr1337 suggested something new to me. Using equal with fuzz value is nice, it would be great it >=  <= would also accept fuzz value...

And what about vlide Watch Window? Can I setup precision for it?
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: MP on November 28, 2017, 01:05:31 PM
Given _Fix's def ...

Code: [Select]
(defun _FuzzyComp ( ? a b prec )
    ;;  (_FuzzyComp =  3.145 3.1454 3) >> T
    ;;  (_FuzzyComp eq 3.145 3.1454 3) >> T
    ;;  (_FuzzyComp <  3.145 3.1454 3) >> nil
    ;;  (_FuzzyComp <= 3.145 3.1454 3) >> T
    (? (_Fix a prec) (_Fix b prec))       
)
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: roy_043 on November 28, 2017, 01:20:11 PM
it would be great it >=  <= would also accept fuzz value...
There is this of course:
Code: [Select]
(>= (- num fuzz) 10.0)
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: dgorsman on November 28, 2017, 01:40:06 PM
Just to make certain on the math - what's the comparison relationship?  Are you checking that the segment length is less than or equal to 12.5?  Or the other way around?

Code: [Select]
(<= SEGMENTLENGTH 12.5)
(<= 12.5 SEGMENTLENGTH)
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: Red Nova on November 28, 2017, 01:57:36 PM
MP - looks nice. Thanks. :)
dgorsman - math I was looking for is for both logics.


Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: MP on November 28, 2017, 02:27:18 PM
You're welcome RN, thx for the thx. :)
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: roy_043 on November 29, 2017, 03:27:24 AM
Hmm... I still think that
Code: [Select]
(<= (- 12.5 fuzz) SEGMENTLENGTH)would be the most obvious solution for the OP's problem.
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: ChrisCarlson on November 29, 2017, 09:09:34 AM
To his second problem, I don't think there is a way to modify the precision of the VLIDE watch window.
Title: Re: "less than or equal to" and similar functions not returning proper value
Post by: Red Nova on November 30, 2017, 11:21:10 AM
Thanx for posts.

roy_043 I was looking for a universal way: +fuzz or -fuzz
Master_Shake  :-(