round a float number to next higher integer

Florent Ghys's icon

Hi, I am sure this is very easy but for some reason I can't make this work.

I need to round incoming floating numbers to the next integers:
For example, an input value of 1.01 would return 2
Basically every float number that is between n and n+1 would return n+1 (int)

any help would be appreciated
thanks
Florent

Florent Ghys's icon
Max Patch
Copy patch and select New From Clipboard in Max.

oh sorry, I knew it wasn't that hard...

Roman Thilenius's icon

[expr (int($f1+1)]

-110

Peter Castine's icon

The problem with Roman's suggestion is that it will "round" an integral value to the next integer. A float value that happens to be an exact integer should remain unchanged.

This would dead easy with the ceil() function. The functions floor() and ceil() are standard functions that round either downwards or upwards. Unfortunately, [expr] doesn't provide either. Well, conversion to int is an implicit floor().

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

Anyway, here's a slightly simpler solution, using only simple arithmetic and comparison objects (no [expr], no [round]).

Florent Ghys's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Thanks Roman and Peter.
You're right Peter, thanks, your solution is more elegant than mine.
I am comparing the incoming number with the rounded number (round 1). If the incoming number is greater than the rounded one, I add 1 to the rounded one and output that value.

Roman Thilenius's icon

oh yeah, that was the part which i missed. i even was about to write (int($f1))+1 first, but then changed it to int($f1+1 to make it look more sexy. :/

Wetterberg's icon

>but then changed it to int($f1+1 to make it look more sexy. :/

Stupid sexy expr.