Compare value with list

Compare value with list

carlos_m_gil_p
Advocate Advocate
1,300 Views
6 Replies
Message 1 of 7

Compare value with list

carlos_m_gil_p
Advocate
Advocate

Hello boys how are you.

I wanted to ask you, how can I compare a numerical value with a list and know if they are the same or not.

 

I have two variables.

 

(setq val 0.0)
(setq lst '(-0.23297 -0.23297 1.0 0.05 4))

 

val contains the value to compare. This value can be any.

lst contains a list of values, this list can have one or more values.

I need it to return T if all the values of the variable lst are equal to the value of the variable val.

 

Beforehand thank you very much.

AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Accepted solutions (2)
1,301 Views
6 Replies
Replies (6)
Message 2 of 7

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try 

(apply '= (cons val lst))

Message 3 of 7

carlos_m_gil_p
Advocate
Advocate

Hello brother, thanks for your help, it worked very well.

Greetings.


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 4 of 7

john.uhden
Mentor
Mentor
Accepted solution

I wrote this earlier today, but something snapped... gone.

 

Real numbers are not as exact as one might think.

If you are looking for matches that are close enough then

(setq fuzz 0.00001) ;; or whatever you like, then

(vl-every '(lambda (x)(equal val x fuzz)) lst)

John F. Uhden

Message 5 of 7

carlos_m_gil_p
Advocate
Advocate

Hello Brother how are you.
I tried your option and it works very well for me.
I think the more exact the better.
I don't know which is better.


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 6 of 7

ronjonp
Mentor
Mentor
IMO, use your acceptable tolerance when comparing real numbers.
Message 7 of 7

carlos_m_gil_p
Advocate
Advocate

Hello guys, thanks for all the opinions and help, I will take them into account.
Greetings.


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes