Loading src/06-types-and-values.md 0 → 100644 +139 −0 Original line number Diff line number Diff line # Types and Values ## Booleans The predefined type `boolean` represents the set of boolean values `true` and `false`. It provides the following operations: - **`not`**: The logical negation. The result is `true` if the operand is `false`, and `false` if the operand is `true`. - **`and`**: The logical conjunction. The result is `true` if both operands are `true`, and `false` otherwise. - **`or`**: The logical disjunction. The result is `true` if at least one operand is `true`, and `false` otherwise. ```ttcn3 test="Verify logical negation" not true == false not false == true ``` ```ttcn3 test="Verify logical conjunction" false and false == false false and true == false true and false == false true and true == true ``` ```ttcn3 test="Verify logical disjunction" false and false == false false and true == true true and false == true true and true == true ``` ## Numbers TTCN-3 provides two numerical types: `integer` and `float`. The predefined type `integer` represents the set of positive and negative whole numbers, including zero. Integers have arbitrary precision; they shall not overflow or underflow. The predefined type `float` represents the set of floating-point numbers and the special float values `not_a_number` and `infinity`. Integer literals are of type `integer`. Float literals are of type `float`. Integer values are not implicitly converted to float values and vice versa. ```ttcn3 test="Assure float literals are not implicitly converted" var integer i := 1.0; // error ``` ```ttcn3 test="Assure integer literals are not implicitly converted" var float f := 0; // error ``` **Operations** - **Negation (`-`)**: The negation of an integer or float. - **Positive (`+`)**: The value of an integer or float. - **Addition (`+`)**: The sum of two integers or two floats. - **Subtraction (`-`)**: The difference of two integers or two floats. - **Multiplication (`*`)**: The product of two integers or two floats. - **Division (`/`)**: The quotient of two integers or two floats. The result of an integer division shall be an integer, truncated towards zero. The result of a float division shall be a float. - **Greater (`>`)**: The comparison of two floats or two integers. The result is `true` if the left operand is greater than the right operand. - **Greater-than (`>=`)**: The comparison of two floats or two integers. The result is `true` if the left operand is greater than or equal to the right operand. - **Less (`<`)**: The comparison of two floats or two integers. The result is `true` if the left operand is less than the right operand. - **Less-than (`<=`)**: The comparison of two floats or two integers. The result is `true` if the left operand is less than or equal to the right operand. ```ttcn3 test="integer division (truncate towards zero)" 5/2 == 2 -5/2 == -2 ``` ```ttcn3 test="float division" 5/2 == 2.5 -5/2 == -2.5 ``` The `integer` type provides the following additional operations: - **Remainder (`rem`)**: The remainder of the division of two integers: $x \rem y := x - y \cdot (\frac{x}{y})$ - **Modulo (`mod`)**: The modulo of the division of two integers. The result has the same sign as the divisor. Formally, mod and rem are defined as follows: ```math x \brem y = x - y * (x/y) x \bmod y = \begin{cases} x \brem |y| & \text{when } x \geq 0 \\ 0 & \text{when } x < 0 \text{ and } x \brem |y| = 0 \\ |y| + x \brem |y| & \text{when } x < 0 \text{ and } x \bmod |y| < 0 \end{cases} ``` ```ttcn3 test="rem" -3 rem 3 == 0 -2 rem 3 == -2 -1 rem 3 == -1 0 rem 3 == 0 1 rem 3 == 1 2 rem 3 == 2 3 rem 3 == 0 ``` ```ttcn3 test="mod" -3 mod 3 == 0 -2 mod 3 == 1 -1 mod 3 == 2 0 mod 3 == 0 1 mod 3 == 1 2 mod 3 == 2 3 mod 3 == 0 ``` Loading
src/06-types-and-values.md 0 → 100644 +139 −0 Original line number Diff line number Diff line # Types and Values ## Booleans The predefined type `boolean` represents the set of boolean values `true` and `false`. It provides the following operations: - **`not`**: The logical negation. The result is `true` if the operand is `false`, and `false` if the operand is `true`. - **`and`**: The logical conjunction. The result is `true` if both operands are `true`, and `false` otherwise. - **`or`**: The logical disjunction. The result is `true` if at least one operand is `true`, and `false` otherwise. ```ttcn3 test="Verify logical negation" not true == false not false == true ``` ```ttcn3 test="Verify logical conjunction" false and false == false false and true == false true and false == false true and true == true ``` ```ttcn3 test="Verify logical disjunction" false and false == false false and true == true true and false == true true and true == true ``` ## Numbers TTCN-3 provides two numerical types: `integer` and `float`. The predefined type `integer` represents the set of positive and negative whole numbers, including zero. Integers have arbitrary precision; they shall not overflow or underflow. The predefined type `float` represents the set of floating-point numbers and the special float values `not_a_number` and `infinity`. Integer literals are of type `integer`. Float literals are of type `float`. Integer values are not implicitly converted to float values and vice versa. ```ttcn3 test="Assure float literals are not implicitly converted" var integer i := 1.0; // error ``` ```ttcn3 test="Assure integer literals are not implicitly converted" var float f := 0; // error ``` **Operations** - **Negation (`-`)**: The negation of an integer or float. - **Positive (`+`)**: The value of an integer or float. - **Addition (`+`)**: The sum of two integers or two floats. - **Subtraction (`-`)**: The difference of two integers or two floats. - **Multiplication (`*`)**: The product of two integers or two floats. - **Division (`/`)**: The quotient of two integers or two floats. The result of an integer division shall be an integer, truncated towards zero. The result of a float division shall be a float. - **Greater (`>`)**: The comparison of two floats or two integers. The result is `true` if the left operand is greater than the right operand. - **Greater-than (`>=`)**: The comparison of two floats or two integers. The result is `true` if the left operand is greater than or equal to the right operand. - **Less (`<`)**: The comparison of two floats or two integers. The result is `true` if the left operand is less than the right operand. - **Less-than (`<=`)**: The comparison of two floats or two integers. The result is `true` if the left operand is less than or equal to the right operand. ```ttcn3 test="integer division (truncate towards zero)" 5/2 == 2 -5/2 == -2 ``` ```ttcn3 test="float division" 5/2 == 2.5 -5/2 == -2.5 ``` The `integer` type provides the following additional operations: - **Remainder (`rem`)**: The remainder of the division of two integers: $x \rem y := x - y \cdot (\frac{x}{y})$ - **Modulo (`mod`)**: The modulo of the division of two integers. The result has the same sign as the divisor. Formally, mod and rem are defined as follows: ```math x \brem y = x - y * (x/y) x \bmod y = \begin{cases} x \brem |y| & \text{when } x \geq 0 \\ 0 & \text{when } x < 0 \text{ and } x \brem |y| = 0 \\ |y| + x \brem |y| & \text{when } x < 0 \text{ and } x \bmod |y| < 0 \end{cases} ``` ```ttcn3 test="rem" -3 rem 3 == 0 -2 rem 3 == -2 -1 rem 3 == -1 0 rem 3 == 0 1 rem 3 == 1 2 rem 3 == 2 3 rem 3 == 0 ``` ```ttcn3 test="mod" -3 mod 3 == 0 -2 mod 3 == 1 -1 mod 3 == 2 0 mod 3 == 0 1 mod 3 == 1 2 mod 3 == 2 3 mod 3 == 0 ```