# MagicPlot Manual

Plotting and nonlinear fitting software

expressions

# Differences

This shows you the differences between two versions of the page.

 expressions [Tue Jan 12 14:15:29 2021]Alexander expressions [Sat Jan 16 17:25:32 2021] (current)Alexander Both sides previous revision Previous revision Sat Jan 16 17:25:32 2021 Alexander Sat Jan 16 17:02:23 2021 Alexander Sat Jan 16 17:01:58 2021 Alexander Tue Jan 12 20:21:29 2021 Alexander Tue Jan 12 14:15:29 2021 Alexander Tue Jan 12 13:37:16 2021 Alexander created Next revision Previous revision Sat Jan 16 17:25:32 2021 Alexander Sat Jan 16 17:02:23 2021 Alexander Sat Jan 16 17:01:58 2021 Alexander Tue Jan 12 20:21:29 2021 Alexander Tue Jan 12 14:15:29 2021 Alexander Tue Jan 12 13:37:16 2021 Alexander created Line 32: Line 32: ==== Using Spaces and Line Breaks ==== ==== Using Spaces and Line Breaks ==== You can freely insert space characters and line breaks in formula, but do not break function names, numbers, operators. You do not need to enter special characters to indicate line break. You can freely insert space characters and line breaks in formula, but do not break function names, numbers, operators. You do not need to enter special characters to indicate line break. + + ===== Local variables in expression ===== + + You can set a local variables in expression. Use semicolon to separate variable assignments and the result expression: ''a=5; a*a + 2*a + 1''. The expression after the last semicolon is the result expression. The variables are calculated in the present order so you need to assign the variable before usage. ===== Functions ===== ===== Functions ===== Line 62: Line 66: ===== Boolean Logic ===== ===== Boolean Logic ===== - MagicPlot can interpret boolean logic expressions. Zero is interpreted as ''false'' and non-zero values are interpreted as ''true'' similarly to C programming language (Note: legacy MagicPlot versions 2.9 and older interpret zero and all negative values as ''false'', this was changed from 3.0 version). You can use simple logical operators which are described below. Use ''1'' as ''true'' and ''0'' as ''false''. + MagicPlot can interpret boolean logic expressions. Zero is interpreted as ''false'' and non-zero values are interpreted as ''true'' similarly to C programming language (Note: legacy MagicPlot versions 2.9 and older interpret zero //and negative// values as ''false'', this was changed in the 3.0 version). You can use simple logical operators which are described below. Use ''1'' as ''true'' and ''0'' as ''false''. ==== The conditional function 'if' ==== ==== The conditional function 'if' ==== Line 76: Line 80: ==== Equality Checking ==== ==== Equality Checking ==== - You have to be careful if you need to check equality of two values. Due to inaccuracy of computer floating-point calculations the result of evaluation is always approximate. For example, result of ''sqrt(3)^2'' is number ''2.9999999999999996'', not exactly ''3''. The expression ''sqrt(3)^2 == 3'' is ''false'' (it returns ''0''). Keep in mind that for convenience MagicPlot rounds numbers when showing on the screen, so this value will be shown as ''3'' in table if the number of shown fractional digits in MagicPlot preferences is not big enough. + You have to be careful if you need to check equality of two values. Due to inaccuracy of computer floating-point calculations the result of evaluation is always approximate. For example, result of ''sqrt(3)^2'' is the number ''2.9999999999999996'', not exactly ''3''. The expression ''sqrt(3)^2 == 3'' is ''false'' (it returns ''0''). Keep in mind that for convenience MagicPlot rounds numbers when showing on the screen, so this value will be shown as ''3'' in table if the number of shown fractional digits in MagicPlot preferences is not big enough. Generally, if you want to check equality of two values you need to use some equality threshold for relative difference. That is, you should compare the modulus of relative difference of two values ''a'' and ''b'' with threshold ''t'': ''if(abs((a-b)/a) < t, ..., ...)''. Generally, if you want to check equality of two values you need to use some equality threshold for relative difference. That is, you should compare the modulus of relative difference of two values ''a'' and ''b'' with threshold ''t'': ''if(abs((a-b)/a) < t, ..., ...)''. Line 85: Line 89: ===== Operators ===== ===== Operators ===== - ^ Operator               ^ Description    ^ Operator                ^ Description              ^ + ^ Operator               ^ Description              ^ ^ Operator                ^ Description              ^ - | ''+''                  | addition       | ''==''                  | equal to                 | + | ''+''                  | addition                 | | ''==''                  | equal to                 | - | ''-''                  | subtraction    | ''!=''                  | not equal to             | + | ''-''                  | subtraction              | | ''!=''                  | not equal to             | - | ''*''                  | multiplication | ''<''                   | less than                | + | ''*''                  | multiplication           | | ''!''                   | logical negation         | - | ''/''                  | division       | ''>''                   | greater than             | + | ''/''                  | division                 | | ''<''                   | less than                | - | ''^'' | power          | ''<='' | less than or equal to    | + | ''%''                  | remainder after division | | ''>''                   | greater than             | - | ''|'' | or             | ''>=''                  | greater than or equal to | + | ''^''                  | exponentiation           | | ''<=''                  | less than or equal to    | - | ''&''                  | and            |                         || + | ''|''                  | logical or               | | ''>=''                  | greater than or equal to | + | ''&''                  | logical and              | |  || ==== Operations Priority ==== ==== Operations Priority ==== - Operators with **lower** precedence value are evaluated **earlier**. You can use brackets to change calculation sequence. - Expression is evaluated left-to-right, excluding repeated exponentiation operator ''^''. The ''^'' operator is [[wp>Operator_associativity|right-associative]] like in Fortran language (evaluated right-to-left; note that in general case ''a^(b^c) ≠ (a^b)^c''). Hence ''a^b^c'' is evaluated as ''a^(b^c)''. + ^ Operations  ^ Precedence  ^ Associativity ^ + | ''function()''               | 1 (is evaluated first) | ---  | + | ''^''                        | 2 | Right-to-left  | + | ''!'', ''-'' (unary minus)   | 3 | ---   | + | ''*'', ''/'', ''%''          | 4 | Left-to-right  | + | ''+'', ''-''                 | 5 | Left-to-right  | + | ''<'', ''>'', ''<='', ''>='' | 6 | Left-to-right  | + | ''=='', ''!=''               | 7 | Left-to-right  | + | ''&''                        | 8 | Left-to-right  | + | ''|''                        | 9 | Left-to-right  | + | ''='' (assignment)           | 10 (is evaluated last) | Left-to-right  | - The reason for exponentiation being right-associative is that a repeated left-associative exponentiation operation would be less useful. Multiple appearances could (and would) be rewritten with multiplication: ''(a^b)^c = a^(b*c)''. + Operators with **lower** precedence value are evaluated **earlier**. You can use brackets to change calculation sequence. - ^ Operations  ^ Precedence  ^ Associativity ^ + Expression is evaluated left-to-right, excluding repeated exponentiation operator ''^''. The ''^'' operator is [[wp>Operator_associativity|right-associative]] like in Fortran language (evaluated right-to-left; note that in general case ''a^(b^c) ≠ (a^b)^c''). Hence ''a^b^c'' is evaluated as ''a^(b^c)''. The reason for exponentiation being right-associative is that a repeated left-associative exponentiation operation would be less useful: Multiple appearances could (and would) be rewritten with multiplication: ''(a^b)^c = a^(b*c)''. - | ''foo()'' (function call) | 1 | ---  | + - | ''^'' | 2 | Right-to-left  | + - | ''-'' (unary minus)    | 3 | ---   | + - | ''*'', ''/''           | 4 | Left-to-right  | + - | ''+'', ''-''           | 5 | Left-to-right  | + - | ''<'', ''>'', ''<='', ''>=''  | 6 | Left-to-right  | + - | ''=='', ''!=''         | 7 | Left-to-right  | + - | ''&''                  | 8 | Left-to-right  | + - |''|''  | 9 | Left-to-right  | + - |''='' (assignment)      | 10 | Left-to-right  | + == Examples == == Examples == Line 120: Line 124: * ''(-3)^2'' returns ''9''. * ''(-3)^2'' returns ''9''. * ''2^2^3'' is equal to ''2^(2^3)'', because ''^'' is right-associative operator. The result is ''256''. * ''2^2^3'' is equal to ''2^(2^3)'', because ''^'' is right-associative operator. The result is ''256''. + + ===== Comments in formulas ===== + + You can insert comments in any formula using ''/*…*/'' notation. + + Press ''Ctrl+/'' on Windows/Linux or ''Cmd-/'' on Mac OS to comment selection. Comments can be multi-line. Note that the single line comments using a symbol at the line start are not supported because line breaks are not taken into account in MagicPlot formula syntax.