<@CALC EXPR=expr
[PRECISION=precision] [FORMAT=format]
[ENCODING=encoding]>
Returns the result of the calculation specified in EXPR.
The 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.
Note: Do not confuse
calculation variables with configuration variables or other Tango variables.
They are only applicable to <@CALC> and do not work with
<@ASSIGN> or <@VAR>.
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.
<@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".
A valid number is a sequence of digits, optionally preceded or trailed by a currency sign (default "$", otherwise set by the configuration variable currencyChar), with any number of thousand separator characters, an optional decimal point, and an exponentiation part. As well, an empty variable or empty string evaluates to zero.
Numbers can be used with any operators and functions, even with the string specific function len, which returns the length of the number converted to a string.
When a number is used in logical expression, any non-zero number is considered true, and zero is considered false.
Logical expressions themselves return "1" if they are true or "0" if they are false.
Two symbolic constants, true and false, which evaluate to "1" and "0", respectively, are provided for convenience.
An empty string evaluates to zero for the purposes of calculation. That is, if the variable foo is empty, the following operations are valid:
<@CALC '@@foo + 1'> OK, returns 1
<@CALC '"" + 1'> OK, returns 1
<@CALC 'mean(@@foo 1)'> OK, returns 0.5
A special case occurs when the thousand separator is set to a space. A number containing a space can be processed if it is a result of a tag evaluation; however, a number literal must be quoted if it includes spaces.
<@ASSIGN NAME=fred VALUE="1 000 000">
<@CALC "@@fred / 100">
Ok, returns 10000.0
<@CALC "@@fred > '1 000'"> Ok, returns 1.0
<@CALC "@@fred > 1 000"> Error
For more information, see currencyChar, decimalChar, DBDecimalChar, and thousandsChar.
The thousands separator, currency sign, and other numerical formats are set by Tango configuration variables. They can be set in various scopes.
<@CALC> treats array references using non-array-specific operators and functions as a numerical value returning the number of rows in the array.
This provides an easy way to verify whether an array is empty or contains a certain value. For example, you can test for the existence of an array variable with <@CALC EXPR="@@array_variable > 0" TRUE="Yes!" FALSE="No such variable.">.
The variable fred contains the following array:
The variable barney contains the following array:
<@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.
For more information, see Calculation Variables.
Note: Single letters must
always be enclosed in quotes in string operations so that they are treated as
letters, and not as 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.
Single letters must always be enclosed in quotes in string operations so that they are treated as letters, and not as calculation variables. For example:
For more information, see beginswith.
<@CALC EXPR="Henry beginswith 'H'"> evaluates the string "Henry" to see if it begins with the string "H" (case-insensitive).
<@CALC EXPR="1234 beginswith H"> evaluates "1234" to see if it begins with the value specified in the calculation variable H (number-to-string conversions are performed).
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>
<@IF>, <@ELSEIF>,
<@ELSEIFEMPTY>, <@ELSEIFEQUAL>, </@IF>
Encoding Attribute
<@FORMAT>
Format Attribute
<@VAR>
Copyright © 1999, Pervasive Software. All rights reserved.