Frag Logo
  Frag Home | Frag SF Project   index | contents | previous | next

Expressions and Mathematical Functions

Expression Evaluation

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.

Expression Operators

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:

There are the following binary operators (in order of precedence groups, from high precedence to low precedence): 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.

Substitutions in Expressions

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])

Usable Data Types in Expressions

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.

Mathematical Functions: The Math Command

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

Syntax

math abs <number>

Description

Calculate absolute value of a number.

Example

The following code returns "1":
math abs -1

math acos

Syntax

math acos <number>

Description

Returns the arc cosine of an angle.

Example

The following code returns "0.0":
math acos 1

math asBoolean

Syntax

math asBoolean <number>

Description

Converts a number into a boolean. All numbers, except for 0, are converted to true. 0 ist converted to false.

Example

The following code returns "true":
math asBoolean 2

math asDouble

Syntax

math asDouble <number>

Description

Converts a number into a Double value.

Example

The following code returns "0.0":
math asDouble 0

math asInt

Syntax

math asInt <number>

Description

Converts a number into an Integer value.

Example

The following code returns "1":
math asInt 1.5

math asLong

Syntax

math asLong <number>

Description

Converts a number into an Long value.

Example

The following code returns "1":
math asLong 1.5

math asin

Syntax

math asin <number>

Description

Returns the arc sine of an angle.

Example

The following code returns "0.0":
math asin 0

math atan

Syntax

math atan <number>

Description

Returns the arc tangent of an angle.

Example

The following code returns "0.0":
math atan 0

math atan2

Syntax

math atan2 <number> <number>

Description

Converts rectangular coordinates (x, y) to polar (r, theta).

Example

The following code returns "1.5707963267948966":
math atan2 1 0

math ceil

Syntax

math ceil <number>

Description

Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.

Example

The following code returns "2.0":
math ceil 1.6

math cos

Syntax

math cos <number>

Description

Returns the trigonometric cosine of an angle.

Example

The following code returns "1.0":
math cos 0

math exp

Syntax

math exp <number>

Description

Returns Euler's number e raised to the power of a value.

Example

The following code returns "2.7182818284590455".
math exp 1

math floor

Syntax

math floor <number>

Description

Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.

Example

The following code returns "3.0":
math floor 3.5

math format

Syntax

math format <pattern> <number>

Description

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.

Examples

The following code returns "177.23":
math format {#.##} 177.23344
The following code returns "00177.2334":
math format {00000.0000} 177.23344

math log

Syntax

math log <number>

Description

Returns the natural logarithm (base e) of a value.

Example

The following code returns "0.0":
math log 1

math pow

Syntax

math pow <number>

Description

Returns the value of the first argument raised to the power of the second argument.

Example

The following code returns "16.0":
math pow 2 4

math random

Syntax

math random

Description

Returns a random double number between 0.0 and 1.0.

Example

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

Syntax

math round <number>

Description

Returns the closest integer to the argument.

Example

The following code returns "1":
math round 1.49
The following code returns "2":
math round 1.500001

math sin

Syntax

math sin <number>

Description

Returns the trigonometric sine of an angle.

Example

The following code returns "0.0":
math sin 0.0

math sqrt

Syntax

math sqrt <number>

Description

Returns the correctly rounded positive square root of a value.

Example

The following code returns "2.0":
math sqrt 4.0

math tan

Syntax

math tan <number>

Description

Returns the trigonometric tangent of an angle.

Example

The following code returns "0.0":
math tan 0

  index | contents | previous | next