round
Returns the integer value nearest to x .
If the fractional part of x is exactly 0.5, the direction of rounding
is undefined and may depend on the host operating system and Csound version.
Syntax
round ( x ) ( init - , control - , or a udio - rate a rg a llowed )
round ( k / i []) ( k - or i - a rrays )
where the argument within the parentheses may be an expression. Value converters perform arithmetic translation from units of one kind to units of another. The result can then be a term in a further expression.
Examples
Here is an example of the round opcode. It uses the file round.csd .
Example of the round opcode. <CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac ;;;realtime audio out
;-iadc ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
;-o round.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
i div init 1
loop :
i number = 9
i 1 = i number / i div
i ro = round ( i 1 )
print i number , i div , i ro ;print number / idiv = result using round
i div = i div + 1
if ( i div <= 10 ) igoto loop
endin
</CsInstruments>
<CsScore>
i 1 0 0
e
</CsScore>
</CsoundSynthesizer>
Its output should include a line like these:
instr 1: inumber = 9.000 idiv = 1.000 ifl = 9.000
instr 1: inumber = 9.000 idiv = 2.000 ifl = 5.000
instr 1: inumber = 9.000 idiv = 3.000 ifl = 3.000
instr 1: inumber = 9.000 idiv = 4.000 ifl = 2.000
instr 1: inumber = 9.000 idiv = 5.000 ifl = 2.000
instr 1: inumber = 9.000 idiv = 6.000 ifl = 2.000
instr 1: inumber = 9.000 idiv = 7.000 ifl = 1.000
instr 1: inumber = 9.000 idiv = 8.000 ifl = 1.000
instr 1: inumber = 9.000 idiv = 9.000 ifl = 1.000
instr 1: inumber = 9.000 idiv = 10.000 ifl = 1.000
Modern Classic
Here is an example for the rounding-group, comparing the different rounding opcodes. It uses the file rounding-group-modern.csd .
Example of the rounding group. <CsoundSynthesizer>
<CsOptions>
-odac ;
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
; by tgrey 2020
instr 1
LoopStart : i = p4
LoopEnd : i = p5
Offset : i = p6
Count : i = init ( LoopStart )
if LoopStart < LoopEnd then ; loop going up
while Count <= LoopEnd do
Val : i = Count + Offset
Round : i = round ( Val )
Int : i = int ( Val )
Floor : i = floor ( Val )
Ceil : i = ceil ( Val )
print ( Val , Round , Int , Floor , Ceil )
Count += 1
od
elseif LoopEnd < LoopStart then ; loop going down
while Count >= LoopEnd do
Val = Count + Offset
Round = round ( Val )
Int = int ( Val )
Floor = floor ( Val )
Ceil = ceil ( Val )
print ( Val , Round , Int , Floor , Ceil )
Count -= 1
od
endif
endin
</CsInstruments>
<CsScore>
i 1 0 0.1 0 10 0.5
i 1 0.2 .1 0 - 10 0.5
e
</CsScore>
</CsoundSynthesizer>
Here is an example for the rounding-group, comparing the different rounding opcodes. It uses the file rounding-group.csd .
Example of the rounding group. <CsoundSynthesizer>
<CsOptions>
-odac ;
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
; by tgrey 2020
instr 1
i LoopStart = p4
i LoopEnd = p5
i Offset = p6
i Count init i LoopStart
if ( i LoopStart < i LoopEnd ) then ; loop going up
while i Count <= i LoopEnd do
i Val = i Count + i Offset
i Round = round ( i Val )
i Int = int ( i Val )
i Floor = floor ( i Val )
i Ceil = ceil ( i Val )
print i Val , i Round , i Int , i Floor , i Ceil
i Count = i Count + 1
od
elseif ( i LoopEnd < i LoopStart ) then ; loop going down
while i Count >= i LoopEnd do
i Val = i Count + i Offset
i Round = round ( i Val )
i Int = int ( i Val )
i Floor = floor ( i Val )
i Ceil = ceil ( i Val )
print i Val , i Round , i Int , i Floor , i Ceil
i Count = i Count - 1
od
endif
endin
</CsInstruments>
<CsScore>
i 1 0 .1 0 10 .5
i 1 .2 .1 0 - 10 .5
e
</CsScore>
</CsoundSynthesizer>
See also
Mathematical Functions
Credits
Author: Istvan Varga
New in Csound 5
2005