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

0 Members and 1 Guest are viewing this topic.

Red Nova

• Newt
• Posts: 38
"less than or equal to" and similar functions not returning proper value
« 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 , 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: 6432
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]
`(equal 12.5 12.500000001 1e-8)`

Windows 10 x64 - AutoCAD /C3D 2018

Custom Build PC

Grrr1337

• Bull Frog
• Posts: 453
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]
`(read (rtos 12.500000001 2 5))`

MP

• Seagull
• Posts: 17015
• brevity != aggression
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 »
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Red Nova

• Newt
• Posts: 38
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: 17015
• brevity != aggression
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))        )`
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

roy_043

• Water Moccasin
• Posts: 1513
• BricsCAD 16
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: 2254
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: 38
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: 17015
• brevity != aggression
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.
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

roy_043

• Water Moccasin
• Posts: 1513
• BricsCAD 16
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.

Master_Shake

• Swamp Rat
• Posts: 1290
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: 38
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