Page 1 of 1

min-max limiter

Posted: Mon Apr 25, 2016 7:13 am
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

Re: min-max limiter

Posted: Mon Apr 25, 2016 8:00 am
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)))

Re: min-max limiter

Posted: Mon Apr 25, 2016 8:21 am
by hds1
sweet.

Thanks a lot for pointing this one out.