min-max limiter

Started by hds1, April 25, 2016, 12:13:46 AM

Previous topic - Next topic

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

#1

(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".



(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

#2
sweet.



Thanks a lot for pointing this one out.