| 1 | |
| 2 | <refentry id="modulus"> |
| 3 | <indexterm id="IndexPercent"><primary>%</primary></indexterm> |
| 4 | <refentryinfo><title>Mathematical Operations:Arithmetic and Logic Operations</title></refentryinfo> |
| 5 | <refmeta> |
| 6 | <refentrytitle>%</refentrytitle> |
| 7 | </refmeta> |
| 8 | |
| 9 | |
| 10 | |
| 11 | <refnamediv> |
| 12 | <refname>%</refname> |
| 13 | |
| 14 | <refpurpose> |
| 15 | Modulus operator. |
| 16 | </refpurpose> |
| 17 | </refnamediv> |
| 18 | |
| 19 | <refsect1> |
| 20 | <title>Description</title> |
| 21 | <para> |
| 22 | Arithmetic operators perform operations of change-sign (negate), don't-change-sign, logical AND logical OR, add, subtract, multiply and divide. Note that a value or an expression may fall between two of these operators, either of which could take it as its left or right argument, as in |
| 23 | <literallayout>a + b * c.</literallayout> |
| 24 | </para> |
| 25 | |
| 26 | <para> |
| 27 | In such cases three rules apply: |
| 28 | </para> |
| 29 | |
| 30 | <para> |
| 31 | 1. * and <emphasis>/</emphasis> bind to their neighbors more strongly than + and −. Thus the above expression is taken as |
| 32 | <literallayout>a + (b * c)</literallayout> |
| 33 | with * taking b and c and then + taking a and b * c. |
| 34 | </para> |
| 35 | |
| 36 | <para> |
| 37 | 2. <emphasis>+</emphasis> and <emphasis>−</emphasis> bind more strongly than &&, which in turn is stronger than ||: |
| 38 | <literallayout>a && b - c || d</literallayout> |
| 39 | is taken as |
| 40 | <literallayout>(a && (b - c)) || d</literallayout> |
| 41 | </para> |
| 42 | |
| 43 | <para> |
| 44 | 3. When both operators bind equally strongly, the operations are done left to right: |
| 45 | <literallayout>a - b - c</literallayout> |
| 46 | is taken as |
| 47 | <literallayout>(a - b) - c</literallayout> |
| 48 | </para> |
| 49 | |
| 50 | <para> |
| 51 | Parentheses may be used as above to force particular groupings. |
| 52 | </para> |
| 53 | |
| 54 | <para> |
| 55 | The operator <emphasis>%</emphasis> returns the value of <emphasis>a</emphasis> reduced by <emphasis>b</emphasis>, so that the result, in absolute value, is less than the absolute value of <emphasis>b</emphasis>, by repeated subtraction. This is technically a remainder, like the % operator in C and similar languages, and not actually a modulus. New in Csound version 3.50. |
| 56 | </para> |
| 57 | </refsect1> |
| 58 | |
| 59 | <refsect1> |
| 60 | <title>Syntax</title> |
| 61 | <synopsis>a <command>%</command> b (no rate restriction)</synopsis> |
| 62 | |
| 63 | <para> |
| 64 | where the arguments <emphasis>a</emphasis> and <emphasis>b</emphasis> may be further expressions. |
| 65 | </para> |
| 66 | </refsect1> |
| 67 | |
| 68 | <refsect1> |
| 69 | <title>Arguments</title> |
| 70 | <para> |
| 71 | The arguments of % can be scalar values or k-rate one |
| 72 | dimensional arrays (vectors), or any combination. If one of the |
| 73 | arguments is an array, so is the value. |
| 74 | </para> |
| 75 | </refsect1> |
| 76 | |
| 77 | <refsect1> |
| 78 | <title>Examples</title> |
| 79 | <para> |
| 80 | Here is an example of the % operator. It uses the file <ulink url="examples/modulus.csd"><citetitle>modulus.csd</citetitle></ulink>. |
| 81 | <example> |
| 82 | <title>Example of the % operator.</title> |
| 83 | <para>See the sections <link linkend="UsingRealTime"><citetitle>Real-time Audio</citetitle></link> and <link linkend="CommandFlags"><citetitle>Command Line Flags</citetitle></link> for more information on using command line flags.</para> |
| 84 | <xi:include href="examples-xml/modulus.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 85 | </example> |
| 86 | </para> |
| 87 | </refsect1> |
| 88 | |
| 89 | <refsect1> |
| 90 | <title>See also</title> |
| 91 | <para><link linkend="MathArtLogic"><citetitle>Arithmetic and Logic Operations</citetitle></link></para> |
| 92 | </refsect1> |
| 93 | |
| 94 | <refsect1> |
| 95 | <title>Credits</title> |
| 96 | <para>Arithmetic on vectors is new in version 6.00</para> |
| 97 | </refsect1> |
| 98 | |
| 99 | </refentry> |