Author Topic: "less than or equal to" and similar functions not returning proper value  (Read 2524 times)

0 Members and 1 Guest are viewing this topic.

Red Nova

  • Newt
  • Posts: 69
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

ronjonp

  • Needs a day job
  • Posts: 7526
Re: "less than or equal to" and similar functions not returning proper value
« Reply #1 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)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: "less than or equal to" and similar functions not returning proper value
« Reply #2 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))
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "less than or equal to" and similar functions not returning proper value
« Reply #3 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
« Last Edit: November 28, 2017, 12:33:17 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Red Nova

  • Newt
  • Posts: 69
Re: "less than or equal to" and similar functions not returning proper value
« Reply #4 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?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "less than or equal to" and similar functions not returning proper value
« Reply #5 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))       
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: "less than or equal to" and similar functions not returning proper value
« Reply #6 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)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: "less than or equal to" and similar functions not returning proper value
« Reply #7 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)
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Red Nova

  • Newt
  • Posts: 69
Re: "less than or equal to" and similar functions not returning proper value
« Reply #8 on: November 28, 2017, 01:57:36 PM »
MP - looks nice. Thanks. :)
dgorsman - math I was looking for is for both logics.



MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: "less than or equal to" and similar functions not returning proper value
« Reply #9 on: November 28, 2017, 02:27:18 PM »
You're welcome RN, thx for the thx. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: "less than or equal to" and similar functions not returning proper value
« Reply #10 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.

ChrisCarlson

  • Guest
Re: "less than or equal to" and similar functions not returning proper value
« Reply #11 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.

Red Nova

  • Newt
  • Posts: 69
Re: "less than or equal to" and similar functions not returning proper value
« Reply #12 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  :-(