Skip to content

int

Extracts an integer from a decimal number.

Syntax

int(x)  (init-rate or control-rate; also works at audio rate in Csound5)

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 int opcode. It uses the file int.csd.

Example of the int 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 int.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

instr 1

icount init 0
loop:
  inum = icount / 3
  inm  = int(inum)
  prints "integer (%f/3) = %f\\n", icount, inm
loop_lt icount, 1, 10, loop

endin
</CsInstruments>
<CsScore>

i 1 0 0
e
</CsScore>
</CsoundSynthesizer>

Its output should include lines like these:

integer (0.000000/3) = 0.000000
integer (1.000000/3) = 0.000000
integer (2.000000/3) = 0.000000
integer (3.000000/3) = 1.000000
integer (4.000000/3) = 1.000000
integer (5.000000/3) = 1.000000
integer (6.000000/3) = 2.000000
integer (7.000000/3) = 2.000000
integer (8.000000/3) = 2.000000
integer (9.000000/3) = 3.000000

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

iLoopStart = p4
iLoopEnd   = p5
iOffset    = p6

iCount init iLoopStart


if(iLoopStart<iLoopEnd) then            ; loop going up
        while iCount <= iLoopEnd do
                iVal = iCount+iOffset
                iRound = round(iVal)
                iInt = int(iVal)
                iFloor = floor(iVal)
                iCeil = ceil(iVal)
                print iVal, iRound, iInt, iFloor, iCeil
                iCount = iCount + 1             
        od

elseif(iLoopEnd<iLoopStart) then        ; loop going down
        while iCount >= iLoopEnd do
                iVal = iCount+iOffset
                iRound = round(iVal)
                iInt = int(iVal)
                iFloor = floor(iVal)
                iCeil = ceil(iVal)
                print iVal, iRound, iInt, iFloor, iCeil
                iCount = iCount - 1             
        od
endif
endin
</CsInstruments>
<CsScore>
i1 0 .1 0 10 .5
i1 .2 .1 0 -10 .5
e
</CsScore>
</CsoundSynthesizer>

See also

Mathematical Functions