### Scaling and rounding in actual maths.

Hey, so recently i’ve been thinking about a lot of the the max objects would be made using mathematical expressions or code, since i’m starting to look into other languages a bit more. Since my maths knowledge is a little awful, I thought i’d ask you guys.

Like I finally found out what [%] does and that it is the whole basis around the [counter] object.

So looking into [scale], I figured out that you take the input number, divide it by the high input value, and times it by the high output value, you get the same effect.

The only feature I can’t seem to replicate is the rounding used in the scale or round object – I can’t figure out what the raw maths equivalent os for

[round 1 @nearest 0] is.

I mean, in lots of code languages there is a round object/function/whatever, but I just want to know what is actually happening in the object.

Thanks guys!

Iyad.

Ah. I think i’ve figured it out.

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
709.3oc0X1saZCCFF93vUgUj1YTTrSbLYZcZ6ZX6rJTkIwEbahMJwzRWUu2m
sSxf1QgDSKEjPArw742uG+5+3oAd9SkqXU9fuBtB348z.OOaUlJ7ZJ64WPWk
lSqrMyOUVTvDJ+g0emhsRYq+WxBlZNWLCjyuiAzerBneoly.OPeT+NUAtpTt
TjAffeHXzRVkBDLA7fr7tp1vkyErTcirwD0T4MRgRPKX194mkbZdayWPUol9
75RVppNKfIvQACAPr4IJzVHLZT.XRyuQrrfKxYJa5.apjmYCtb5sWLNvei9s
h+Ga+BQ5.8u.HWpZiPfoxmGLv7X3AxveuMnUkRyY.K4p.uibBuFSPbbuwDI4
CGS2jK0Aw2krCUaBhiLugCrdgfdjcwwqCdotSUrxqYB5zba2GzmLuczotJ0i
KX0RzjdTsG.3OkJl4Cl3.gDrGzp8+7QWbHLKzZIBQiv6hYnsvLRubDv2hKbi
q9cjFeA.G4DPzyGzLHIwxC7t8PagGD7mLON34OwgNN8gbdL8QImMSKocrHYj
k.Hz3VDzSPDkzEP7QaD1adhvn5sKibMOiOKxSXB4.yyvSg77MVn66WBBFgca
9dsA.VuXWT+WrKL4zbw+KMLw2sCRR1.Igi6ORNQ2O7aN6RZVN7.PxY7Vhs9g
HjiaJhFeNelRmOEELo9NFvPGOGEJ3b1zXyWhq2CAR9T8LVcZur4q9OBropo9
WhsJ4xxz131N1AVmtY567yETEWJ1rQieQilyyxXhMyuBd1Bod3qQDnfZGk9N
JaDZdkAKYu8kM6plMLe+ZNXOZ98SOuBOaWOl8eOR54Uc010i4.gGK8f6hdRN
wFuvmX5gb7Fu5x7KyAsOR5ItK5I934mIwcQOjSK8PNd9YBtq7A5fdp2xitXw
8rxplXZkh9LA2JKMEiGZKxE0EsQzujcOus83Aln87f+BQpUYJC
-----------end_max5_patcher-----------
```

I was being stupid again.

ALTHOUGH, i’d love to know how any of the other max objects actually function through the land of maths if you guys have any examples.

This intrigues me.

Thanks.

Cool, thats for that, very helpull.

I was also wondering if [==], [< =] and [>=] could somehow be put into maths speak?

Perhaps the [>= 4] object would be along these lines?

[expr ($i1/4)/($i1/4)]

Thanks.

comparison operators can not be put to a lower level or something, it is kinda the

smallest possible part, almost machine language.

but expr can use these guys, too.

in expr it will take up more CPU than with the >= object, but of course, several of

them can be combined in one object that way, which can, otoh, also save some

CPU, at least when run in high priority (==when the input comes a from a metro or line)

[expr $i1>=4] will return 0 or 1, just like the >= operator external.

you can combine several comparisons using * as logical-and:

[expr ($i1>=4) * (($i1%12)==1)] will return true only if both conditions are met.

and of course you can combine it with any other functions.

use addition for the true and false version of the calculation, and multiplication

to bind them to the condition (==comparison operator).

[expr ($i1+5) * ($i1>=4) + ($i1) * ($i1<4)]

"take input and add five, but only when the input is greater or equal to 4, else output is input":

not even [if] can do that in one line.

-110

now the other way around! i mean, come on guys we should finally write the principia mathematica patch! :)

Integration and derivation are pretty simple in a sample-based system, so you’re on the way… (+= and current-previous)

One C-style math that’s not available in expr (though it is in gen~) is floor/ceil. Floor always rounds towards negative infinity, and ceil always rounds up towards positive infinity. They’re handy for rounding algorithms because weird things happen with negative numbers. (Test your rounding code with negative numbers and you’ll see)

One other math that’s really good to know about is Horner’s rule for evaluating polynomials:

It’s way more efficient to do:

d + x*(c + x*(b + x*a)) // 3 multiplies, 3 adds

instead of:

a*x^3 + b*x^2 + c*x + d // 8 multiplies(!), 3 adds

On a practical note, it means that the number of operations to compute a polynomial grows linearly rather than exponentially, so you can compute a 10th order Chebyshev polynomial using 10 multiply-add operations, which is a good thing.

Forums > MaxMSP