Debian Codegraph @

score/eval.xml

raw symbols SCIP
1 
2 <section id="ScoreEval">
3 <title>Evaluation of Expressions</title>
4
5 <para>
6 In earlier versions of Csound the numbers presented in a score were used as given. There are occasions when some simple evaluation would be easier. This need is increased when there are macros. To assist in this area the syntax of arithmetic expressions within square brackets [ ] has been introduced. Expressions built from the operations +, -, *, /, % ("modulo"), and ^ ("power of") are allowed, together with grouping with ( ). Unary minus and plus are also supported. The expressions can include numbers, and naturally macros whose values are numeric or arithmetic strings. All calculations are made in floating point numbers. The usual precedence rules are followed when evaluating: expressions within parantheses ( ) are evaluated first and ^ is evaluated before *, /, and % which are evaluated before + and -.
7 </para>
8
9 <para>
10 In addition to arithmetic operations, the following bitwise logical operators are also available: &amp; (AND), | (OR), and # (XOR, exclusive-OR). These operators round their operands to the nearest (long) integer before evaluating. The logical operators have the same precedence as the *, /, and % arithmetic operators.
11 </para>
12
13 <para>
14 Finally, the tilde symbol &tilde; can be used in an expression wherever a number is permissible to use. Each &tilde; will evaluate to a random value between zero (0) and one (1).
15 </para>
16
17 <simplesect>
18 <title>Example</title>
19 <para>
20 <informalexample>
21 <programlisting>
22 <emphasis role="stamnt">r</emphasis>3 CNT
23
24 <emphasis role="stamnt">i</emphasis>1 0 [0.3*$CNT.]
25 <emphasis role="stamnt">i</emphasis>1 + [($CNT./3)+0.2]
26
27 <emphasis role="stamnt">e</emphasis></programlisting>
28 </informalexample>
29
30 As the three copies of the section have the macro $CNT. with the different values of 1, 2 and 3, this expands to
31
32 <informalexample>
33 <programlisting>
34 <emphasis role="stamnt">s</emphasis>
35 <emphasis role="stamnt">i</emphasis>1 0 0.3
36 <emphasis role="stamnt">i</emphasis>1 0.3 0.533333
37 <emphasis role="stamnt">s</emphasis>
38 <emphasis role="stamnt">i</emphasis>1 0 0.6
39 <emphasis role="stamnt">i</emphasis>1 0.6 0.866667
40 <emphasis role="stamnt">s</emphasis>
41 <emphasis role="stamnt">i</emphasis>1 0 0.9
42 <emphasis role="stamnt">i</emphasis>1 0.9 1.2
43 <emphasis role="stamnt">e</emphasis></programlisting>
44 </informalexample>
45
46 This is an extreme form, but the evaluation system can be used to ensure that repeated sections are subtly different.
47 </para>
48
49 <para>
50 Here are some simple examples of each operator:
51
52 <informalexample>
53 <programlisting>
54 <emphasis role="stamnt">i</emphasis>1 0 1 [ 110 + 220 ] ; evaluates to 330
55 <emphasis role="stamnt">i</emphasis>1 + . [ 330 - 55 ] ; 275
56 <emphasis role="stamnt">i</emphasis>1 + . [ 44 * 10 ] ; 440
57 <emphasis role="stamnt">i</emphasis>1 + . [ 1100 / 2 ] ; 550
58 <emphasis role="stamnt">i</emphasis>1 + . [ 5 ^ 4 ] ; 625
59 <emphasis role="stamnt">i</emphasis>1 + . [ 5660 % 1000 ] ; 660
60 <emphasis role="stamnt">i</emphasis>1 + . [ 110 &amp; 220 ] ; 76
61 <emphasis role="stamnt">i</emphasis>1 + . [ 110 | 220 ] ; 254
62 <emphasis role="stamnt">i</emphasis>1 + . [ 110 # 220 ] ; 178
63 <emphasis role="stamnt">i</emphasis>1 + . [~] ; random between 0-1
64 <emphasis role="stamnt">i</emphasis>1 + . [~ * 4 + 1] ; random between 1-5
65 <emphasis role="stamnt">i</emphasis>1 + . [~ * 95 + 5] ; random between 5-100
66
67 <emphasis role="stamnt">i</emphasis>1 + . [ 8 / 2 * 3 ] ; 12
68 <emphasis role="stamnt">i</emphasis>1 + . [ 4 + 3 - 2 + 1 ] ; 6
69 <emphasis role="stamnt">i</emphasis>1 + . [ 4 + 3 * 2 + 1 ] ; 11
70 <emphasis role="stamnt">i</emphasis>1 + . [(4 + 3)*(2 + 1)] ; 21
71
72 <emphasis role="stamnt">i</emphasis>1 + . [ 2 * 2 &amp; 3 ] ; 4
73 <emphasis role="stamnt">i</emphasis>1 + . [ 3 &amp; 2 * 2 ] ; 0
74 <emphasis role="stamnt">i</emphasis>1 + . [ 4 | 3 * 3 ] ; 13
75 </programlisting>
76 </informalexample>
77 </para>
78 </simplesect>
79 <simplesect>
80 <title>The &commat; operator</title>
81 <para>
82 New in Csound version 3.56 are <emphasis>&commat;</emphasis><emphasis>x</emphasis> (next power-of-two greater than or equal to <emphasis>x</emphasis>) and <emphasis>&commat;&commat;</emphasis><emphasis>x</emphasis> (next power-of-two-plus-one greater than or equal to <emphasis>x</emphasis>).
83
84 <indexterm id="IndexCommat"><primary>&commat;</primary></indexterm>
85 <indexterm id="IndexCommatCommat"><primary>&commat;&commat;</primary></indexterm>
86 </para>
87 <informalexample>
88 <programlisting>
89 [ @ 11 ] will evaluate to 16
90 [ @@ 11 ] to 17</programlisting>
91 </informalexample>
92 </simplesect>
93 <simplesect>
94 <title>Credits</title>
95 <para>Author: &namejohn;</para>
96 <para>University of Bath/Codemist Ltd.</para>
97 <para>Bath, UK</para>
98 <para>April, 1998 (New in Csound version 3.48)</para>
99 </simplesect>
100 </section>