min-max limiter

For the Compleat Fan
Post Reply
hds1
Posts: 28
Joined: Thu Mar 20, 2014 5:02 pm

min-max limiter

Post by hds1 »

Hello,

i couldn't find a function in newlisp to limit a value between upper and lower limits.
i.e. something like this:

"return x < some_minimum ? some_minimum : x > some_maximum ? : some_maximum : x;"

Reason is that i'am working a lot with sin, cos and the brothers and i often need to limit my floats for example to precisly -1 or 1.

Thanks and Regards
Heiko

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: min-max limiter

Post by TedWalther »

Code: Select all

(cond ((> x some_max) x_max) ((< x some_min) x_min) (true x))
If you use this idiom enough, you may want to wrap it into a function, call it "bandpass".

Code: Select all

(define (bandpass x xmin xmax)
    (cond ((> x xmax) xmax) ((< x xmin) xmin) (true x)))
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

hds1
Posts: 28
Joined: Thu Mar 20, 2014 5:02 pm

Re: min-max limiter

Post by hds1 »

sweet.

Thanks a lot for pointing this one out.

Post Reply