Rounding up floats in Max

Dr. Spankenstein's icon

I've looked through the tutorials patches such as 08 maths in max and 10 number boxes but cant find anything related to rounding up/down floats to the nearest whole number...

I.e. 5.5 to 5.9 rounds up to 6.
5.1 to 5.4 rounds down to 5.

Am I not looking hard enough, can someone please help?

Thanks

Rhys

Emmanuel Jourdan's icon
Chris Muir's icon

At 10:10 AM -0700 2/12/07, Rhys Perkins wrote:
>I've looked through the tutorials patches such as 08 maths in max and 10 number boxes but cant find anything related to rounding up/down floats to the nearest whole number...

Add .5 and truncate:

Max Patch
Copy patch and select New From Clipboard in Max.

--
Chris Muir | "There are many futures and only one status quo.
cbm@well.com | This is why conservatives mostly agree,
http://www.xfade.com | and radicals always argue." - Brian Eno

mzed's icon

Quote: Chris Muir wrote on Mon, 12 February 2007 10:27
----------------------------------------------------
> At 10:10 AM -0700 2/12/07, Rhys Perkins wrote:
> >I've looked through the tutorials patches such as 08 maths in max and 10 number boxes but cant find anything related to rounding up/down floats to the nearest whole number...
>
> Add .5 and truncate:
>

Unless it's a negative number:

Max Patch
Copy patch and select New From Clipboard in Max.

Peter Castine's icon

On 12-Feb-2007, at 18:10, Rhys Perkins wrote:
> I.e. 5.5 to 5.9 rounds up to 6.
> 5.1 to 5.4 rounds down to 5.
>
> Am I not looking hard enough, can someone please help?

An easy way is to use lp.scampi from the Litter Power Starter Pack.

[lp.scampi 1 0 round] will do the math. Including correct handling of
neative values.

Available below.

Best, Peter

-------------- http://www.bek.no/~pcastine/Litter/ -------------
Peter Castine +--> Litter Power & Litter Bundle for Jitter
Universal Binaries on the way
iCE: Sequencing, Recording &
Interface Building for |home | chez nous|
Max/MSP Extremely cool |bei uns | i nostri|
http://www.dspaudio.com/ http://www.castine.de

Eamonn Doyle's icon

[expr int($f1+0.5)]

Peter Castine's icon

On 15-Feb-2007, at 11:00, Eamonn Doyle wrote:

> [expr int($f1+0.5)]

As has been pointed out, this is broken for negative input.

-------------- http://www.bek.no/~pcastine/Litter/ -------------
Peter Castine +--> Litter Power & Litter Bundle for Jitter
Universal Binaries on the way
iCE: Sequencing, Recording &
Interface Building for |home | chez nous|
Max/MSP Extremely cool |bei uns | i nostri|
http://www.dspaudio.com/ http://www.castine.de

J no.e Parker's icon

thanks for sharing the expression, Eamonn Doyle!

Roman Thilenius's icon

never underestimate code from 2007

Peter Castine's icon

Actually, code from about 1948, but never mind… -)

Roman Thilenius's icon

normally i should trust you, but isnt it more like 5000 years old?

Peter Castine's icon

The operative word was "code," so I was pulling a number out of a hat for sometime between Zuse's Plankalkül and FORTRAN.

Prior to coding, I was more familiar with rounding defined as an if-then calculation (if fractional part ≥ 0.5 then…), rather than add-one-half-and-truncate (with special handling for negatives), so I didn't want to go back much earlier than early programming languages that might truncate on float-to-int typecasts. Also, I didn't want to rub it in too much, because that makes me look like a wanker. Somebody else has already claimed that task ;-)

Peter Castine's icon

OK, OK, go back to the Seive of Eratosthenes if you must (although I'm not aware of even Nichomachus or Euclid discussing truncate-on-typecast-to-int). Perhaps Brahmagupta or Muḥammad al-Khwārizmī? That would get some non-Westerners into the deal, although they came about a thousand years after the Greeks.