<@CALC>Syntax<@CALC EXPR=expr [PRECISION=precision] [FORMAT=format][ENCODING=encoding]> DescriptionReturns the result of the calculation specified in EXPR. Basic FunctionalityThe expression may contain numbers (including numbers in scientific notation); any of six arithmetic operations--multiplication (*), division (/), modulo (%), power (^), addition (+), and subtraction (-); parentheses for controlling the order of operations; mathematical functions; string functions; logical operations; comparison operations; calculation variables (A-Z); and sub-expressions.
If the expression contains any spaces--except for spaces within embedded meta tags--it must be quoted. The optional PRECISION attribute is an integer that controls the number of decimal places displayed in the result. The default precision is the maximum required for accuracy of the result. If an error is encountered while the expression is parsed or computed, the computation is halted and the tag is substituted with relevant error information. Examples<@CALC EXPR="<@POSTARG NAME='calculateThis'>" PRECISION="4"> This evaluates the contents of the form field specified--calculateThis--to four decimal places of precision. If the field contained "8*9+8/2", for example, the tag would evaluate to "76.0000". Advanced Functionality and Calculation Variables
|
1 | 2 |
3 | 4 |
The variable barney contains the following array:
1 | 2 |
5 | 6 |
7 | 8 |
<@IF EXPR="@@fred > @@barney" TRUE="true!" FALSE="alas"> returns "alas".
The calculator can accept hexadecimal, octal, and binary numbers. The num function converts strings representing hexadecimal, octal and binary numbers to decimal numbers, and the result of the conversion can be used anywhere where a number is used. The following table specifies the conversion rules.
Note:
If a decimal number is
passed to this function, it either yields an error or an incorrect
result
Prefix |
Valid Symbols |
Converted As |
Examples |
For example, all the following expressions generate errors:
num(0x123fga)
ERROR: letter g is invalid
num(012380)
ERROR: digit 8 is invalid
num(123)
ERROR: digits 2 and 3 are invalid
Any Tango meta tag that does not evaluate to a valid number or array reference is considered a string. No additional quoting is required. There is a single exception to this rule, further explained in "Meta Tag Evaluation" .
Strings can be used only in comparison operations, contains clauses or as arguments to the len function. A string literal--that is, a string, directly included in the expression--must be enclosed in single quotes if it contains spaces, special characters or starts with a digit.
Single letters must always be enclosed in quotes in string operations so that they are treated as letters, and not as calculation variables.
For more information, see “Calculation Variables”.
The following examples show string comparisons. If a string literal contains a single quote or a backslash, it must be escaped with a backslash.
<@ASSIGN NAME=name VALUE="John
Lennon">
<@CALC EXPR="@@name=John"> false
<@CALC EXPR="@@name=John Lennon"> ERROR
<@CALC EXPR="@@name='John Lennon'">
true
<@CALC EXPR="@@name='John*'">
true
<@ASSIGN NAME=name VALUE="John's
trousers">
<@CALC EXPR="@@name=John*" true
<@CALC EXPR="@@name='John\'s trousers'">
true
<@CALC EXPR="@@name='John's'"> ERROR
<@ASSIGN NAME=dir VALUE="C:\test">
<@CALC EXPR="@@dir='C:\test'"> false
<@CALC EXPR="@@dir='C:\\test'"> true
When a string is encountered on one side of the comparison operation, the other operand is forced to a string, too. For example:
2.15 <='abba'
'123.456.78.12'=@@ip_address
Function len returns the length of the string, so the result of this operation can be used anywhere a number can be used. Strings can not be assigned to calculation variables.
For example, these are valid expressions:
ABBA='BLACK SABBATH' false
len(JOHN LENNON) + len(FREDDY MERCURY) - 5 > 0 true
a :=ABBA ERROR: cannot
assign string
FREDDY < 0 ERROR: cannot compare string
and number
and this tag returns true although you may expect it to return false:
Note:
A single letter on both
sides of the comparison operator evaluates to a calculation variable,
meaning a number comparison is performed.
String comparisons using <@CALC> are case insensitive.
A calculation variable is a single case-insensitive letter (A-Z) that can be assigned a numeric value and used in subsequent operations. You can write small programs inside the tag with calculation variables and statement separators, or put a program in a separate file and use <@INCLUDE> to calculate the result.
The following table shows predefined calculation variables. You may use these values in your programs, or have any of these calculation variables reassigned with any other value.
Variable |
Meaning |
Value |
The following table shows the operators listed in order of increasing precedence. Operators having the same precedence, for example, plus and minus, are not separated by a rule.
Note:
The beginswith
operator should be used instead of a trailing asterisk as a wildcard
in comparisons.The use of asterisks as wildcards is deprecated and
will be removed in a future release.
Each built-in function expects either a single numeric argument, or a space-separated list of mixed numeric and array arguments, or a string. It is an error to specify an argument of the wrong type to a function. If an array, specified as an argument to a function, contains non-numeric elements, these elements are ignored without any error diagnostics.
The following tables list all built-in functions.
Function |
Meaning and Return Value |
Arguments and Usage |
converts a string, representing a hexadecimal, octal, or binary number into a number |
Function |
Meaning and Return Value |
Arguments and Usage |
Avar=((A1-Amean)2+((A2-Amean)2+...+((An-Amean)2)/(n-1) returns the (squared) variance of the elements |
The contains operator has the following syntax:
<@VAR NAME="array"> contains number or string
This operator checks if the specified number or string is contained in the array. The string should be enclosed in quotes, if it contains any non-alphanumeric characters. The operator returns "1" if the element is found, or "0" otherwise.
For example, the following expression, which uses the <@IF> meta tag, returns "Cool" if "Queen" is found in the CDs array, and "Too Bad" if it is not.
<@IF EXPR="<@VAR NAME=CDs> contains Queen" TRUE=Cool FALSE="Too bad">
The foreach operator has the following syntax:
<@VAR array> foreach {statement; ...}
For
more information, see "<@ARRAY>".
For more information, see "<@ASSIGN>".
This operator steps through the elements of an array and it assigns
and it executes the statements inside the braces "{ }"for each element. All non-numeric elements are interpreted as zeroes.
The operator returns the last calculated value of the expression.
The values of "X, I, J" are restored upon the exit from the foreach operator. For example, if array CDs is initialized as follows:
<@ASSIGN NAME="CDinitValue"
VALUE="AC/DC,Scorpions,Deep Purple,Black
Sabbath,Queen;19.50,22.50,22.50,17.90,29.00">
<@ASSIGN NAME="CDs" VALUE="<@ARRAY ROWS='2'
COLS='5' VALUE=@@CDinitValue CDELIM=','
RDELIM=';'>">
then the following program prints the name of the most expensive CD:
<@VAR NAME=CDs[1,<@CALC "t :=1; p :=0.0;
<@VAR NAME=CDs> foreach
{ t :=(p < x)? j: t; p :=(p < x)? x: p; }; t">]>
There are two special cases when a meta tag is not treated as a string. Consider the following two examples:
<@CALC EXPR="<@POSTARG
NAME=prog>">
<@CALC EXPR="<@INCLUDE FILE=myprog>">
If the post argument prog contains an expression submitted by a user, or the file myprog contains an expression to be calculated, one would expect <@CALC> to produce the result of the calculation. The rule is, if the expression contains a single meta tag, such an expression is fully evaluated by the calculator, rather than treated as a string.
Parentheses can be used to order the evaluation of expressions that otherwise are evaluated in the order specified in the Operators table ("Operators" ). For example:
This example evaluates to "23".
This example evaluates to "35".
A more complex example can be constructed using different operators and nested parentheses:
<@CALC EXPR="(<@ARG
_function> = 'detail') and ((len(<@ARG id>) != 0 and <@ARG
mode>='abs')
or (<@ARG mode>='next' or <@ARG mode>='prev'">))>
This tag evaluates to "1" (true) if the _function argument is equal to "detail" and any one of the following conditions are met:
<@ARRAY>
Encoding Attribute
<@FORMAT>
Format Attribute
<@IF>, <@ELSEIF>,
<@ELSEIFEMPTY>, <@ELSEIFEQUAL>, </@IF>
<@VAR>
Copyright © 1998, Pervasive Software Inc. All rights reserved.