Expressions and Mathematical Functions
There are a number of commands that expect an expression as an argument,
and evaluate it later in time. In these commands, expressions are typically
provided in curly braces. Examples of such commands are the control structures,
such as foreach, for, while, and
if (see Section Control Structures).
Alternatively, expressions can be directly evaluated. That is, the expression
is evaluated, and, after the evaluation took place, the expression is replaced
with the result of the expression. This is done by providing the expression inside
of round braces: (...), which trigger an expression substitution.
Here is an example that contains both kinds of expression evaluation.
In the example a while loop, which prints ten lines of the form
x is 0, x is 1, ..., is defined.
set x 0
while {$x < 10} {
puts "x is $x"
set x ($x + 1)
}
In the expression
argument of while, the expression is given in curly braces, because while
does not need to evaluate the expression directly, but must evaluate the expression multiple
times. If we would have used while ($x < 10) ... instead, while would
receive the value of the expression, at the point in time when while is reached in the control
flow (which is true in this example). Hence, the loop would run forever. But
instead we use while {$x < 10} ..., which passes the expression code block:
"$x < 10" to the while command. The while command then can evaluate the expression
for every iteration of the while loop.
In the second expression in the while body, $x + 1, we require direct evaluation
of the expression, because the result of the expression
must be written into the variable x.
Hence round braces are used for evaluating the second expression.
Frag offers the usual expression operators, as they can be found in many other
languages, such as Java or C.
There are the following unary operators:
- Unary Minus: -
- Unary Plus: +
- Unary Not: !
- Bitwise Not: ~
There are the following binary operators (in order of precedence groups, from high precedence to low precedence):
- Multiply: *
- Divide: /
- Modulo: %
- Bitshift Left: <<
- Bitshift Right: >>
- Less: <
- Less Equal: <=
- Greater: >
- Greater Equal: >=
An operator with higher precedence has greater binding power.
For example, the expression
set x (1 - 1 * 19)
is equivalent to:
set x (1 - (1 * 19))
because "*" has a higher precedence than "-".
As usual (round) braces can be used to control the precedence of operators.
For instance, the expression:
set x ((1 - 1) * 19)
evaluates the "-" operator first.
Inside of an expression, code substitution, escape (backslash) substitution,
and variable substitution are supported. Here is an example:
set z 3
set x ($z + [get z])
In expressions, all kinds of data types can be used. However, not all operators are
defined for all data types. Almost all operators are applicable for
numbers, but some operators do not support double values as operands. Also boolean values
are not always supported. If a data type is unknown, its string value is used.
Strings can be used inside of an expression, but only the operators "==" and "!="
work for strings. If a wrong data type is chosen, an exception is raised.
The predefined math command contains all kinds of mathematical functions that
can be used in Frag invocations and in expressions through code substitution.
For instance, the following code uses math sqrt in an expression:
set x (2 + [math sqrt 2.0])
The general syntax of math is:
math <math function name> ?args ...?
Below you find a complete reference of the mathematical functions provided by math.
math abs <number>
Calculate absolute value of a number.
The following code returns "1":
math abs -1
math acos <number>
Returns the arc cosine of an angle.
The following code returns "0.0":
math acos 1
math asBoolean <number>
Converts a number into a boolean. All numbers, except for 0, are
converted to true. 0 ist converted to false.
The following code returns "true":
math asBoolean 2
math asDouble <number>
Converts a number into a Double value.
The following code returns "0.0":
math asDouble 0
math asInt <number>
Converts a number into an Integer value.
The following code returns "1":
math asInt 1.5
math asLong <number>
Converts a number into an Long value.
The following code returns "1":
math asLong 1.5
math asin <number>
Returns the arc sine of an angle.
The following code returns "0.0":
math asin 0
math atan <number>
Returns the arc tangent of an angle.
The following code returns "0.0":
math atan 0
math atan2 <number> <number>
Converts rectangular coordinates (x, y) to polar (r, theta).
The following code returns "1.5707963267948966":
math atan2 1 0
math ceil <number>
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
The following code returns "2.0":
math ceil 1.6
math cos <number>
Returns the trigonometric cosine of an angle.
The following code returns "1.0":
math cos 0
math exp <number>
Returns Euler's number e raised to the power of a value.
The following code returns "2.7182818284590455".
math exp 1
math floor <number>
Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
The following code returns "3.0":
math floor 3.5
math format <pattern> <number>
This method applies a decimal format pattern to a number. This is internally done using the
applyPattern method of Java's DecimalFormat class. Refer to the page
Class DecimalFormat
for a reference of the pattern formats that are supported.
The following code returns "177.23":
math format {#.##} 177.23344
The following code returns "00177.2334":
math format {00000.0000} 177.23344
math log <number>
Returns the natural logarithm (base e) of a value.
The following code returns "0.0":
math log 1
math pow <number>
Returns the value of the first argument raised to the power of the second argument.
The following code returns "16.0":
math pow 2 4
math random
Returns a random double number between 0.0 and 1.0.
The two expressions in the following list are always true, but the
numbers produced by math random change:
list build ([math random] <= 1) ([math random] >= 0)
math round <number>
Returns the closest integer to the argument.
The following code returns "1":
math round 1.49
The following code returns "2":
math round 1.500001
math sin <number>
Returns the trigonometric sine of an angle.
The following code returns "0.0":
math sin 0.0
math sqrt <number>
Returns the correctly rounded positive square root of a value.
The following code returns "2.0":
math sqrt 4.0
math tan <number>
Returns the trigonometric tangent of an angle.
The following code returns "0.0":
math tan 0