Get non-fractional/round down to nearest int?

Hey all,

Curious what the most efficient way to round to to the nearest int value is in csound. Thanks in advance!

Cheers

http://www.csounds.com/manual/html/int.html

Like this?

1 Like

Thank you, do not know how I missed that one haha. Much thanks!

but note that int() does not return the nearest integer, but the integer
part of a fractional number. rounding is done with round():
int(1.6) -> 1
round(1.6) -> 2

1 Like

The docs says this about the round() opcode:

if the fractional part of x is exactly 0.5, the direction of rounding is undefined

Okey that’s fine, but what if you actually want to round up/down when the fractional part is exactly 0.5? Any tips on how to do this or any opcodes?

hmm — not tested but i think something like this should work:

iNum = 5.5
if frac(iNum) == 0.5 then
iNum = floor(iNum)
endif

or in one line:

iNum = (frac(iNum) == 0.5) ? floor(iNum) : iNum

joachim

Thanks, those are some useful opcodes I’ve never seen before!