[ Prev | Index | Next ]
Japanese English

Arithmetic Operations

In this section, we will discuss how to perform arithmetic operations, such as addition, subtraction, multiplication, and division.

Sponsored Link


Arithmetic Operators

In programming, you can use arithmetic operators to perform various calculations. To perform arithmetic operations, we use arithmetic operators. An "operator" is a symbol used to perform operations, such as "+" for addition or "-" for subtraction. VCSSL supports the following arithmetic operators:

Symbol Meaning Supported Types Details
 + Addition int,
float,
complex,
varint,
varfloat,
varcomplex,
string
Performs addition of numbers. For the string type, it concatenates strings.
 - Subtraction int,
float,
complex,
varint,
varfloat,
varcomplex
Performs subtraction of numbers.
 * Multiplication int,
float,
complex,
varint,
varfloat,
varcomplex
Performs multiplication of numbers.
 / Division int,
float,
complex,
varint,
varfloat,
varcomplex
Performs division of numbers.
 % Remainder int,
varint
Calculates the remainder when dividing numbers.
 ** Exponentiation float,
varfloat
Performs exponentiation. Requires importing the Math library.

Note about Exponentiation

In earlier versions of VCSSL (prior to 2.1), the caret symbol (^) was used for exponentiation. However, this symbol is commonly associated with bitwise XOR in other C-like languages. Therefore, starting from VCSSL 2.1, it is recommended to use the double asterisk (**) symbol instead.

Additionally, at present, exponentiation between integers is performed after converting them to floating-point numbers, resulting in a float-type output. To avoid confusion, integer-to-integer exponentiation is deprecated from VCSSL 3.0 onwards. It is recommended to explicitly cast operands to floating-point numbers before exponentiation.

While integer-to-integer exponentiation is currently compatible, there is a possibility that future specifications may modify it to return integer results.

Compound Assignment Operators and Increment/Decrement Operators

For frequently used operations that involve both arithmetic operation and assignment, special operators (referred to as "compound assignment operators") are provided as follows. These operators are widely adopted in many C-like languages.

Symbol Meaning Example
 += Add the right value to the left value and assign the result to the left value. a += b ;
( a is assigned the result of a+b )
 -= Subtract the right value from the left value and assign the result to the left value. ( a is assigned the result of a-b )
 *= Multiply the left value by the right value and assign the result to the left value. a *= b ;
( a is assigned the result of a*b )
 /= Divide the left value by the right value and assign the result to the left value. a /= b ;
( a is assigned the result of a/b )
 ++ Increment the left or right value by 1 and assign the result to that value. a ++ ;
( a is assigned the result of a+1 )
 -- Decrement the left or right value by 1 and assign the result to that value. a -- ;
( a is assigned the result of a-1 )

The last two operators, ++ and --, are used extensively for loop counter control, making their usage very common. They are generally referred to as "increment" and "decrement" operators, respectively.

When assigning the result of an increment to another variable, such as 'a = ++b;' or 'a = b++;', the prefix increment ('++b') assigns the value after the increment, while the postfix increment ('b++') assigns the original value (before applying the increment). This principle applies similarly to decrements.

Performing Addition

As an example of using arithmetic operators, let's perform addition operations. Try the following code:

- Execution Result -

2

When you execute this program, "2" will be displayed in the VCSSL console. Now, what happens when you do this:

- Execution Result -

2.0

This time, "2.0" will be displayed in the message window.

In the first example, it was "2" and an integer, but in the second example, it became "2.0" and a number with a decimal point. This means that the result of the first operation is an integer value, while the result of the second operation becomes a value with a decimal point.

Thus, the result type of an arithmetic operation varies based on the operand types. As explained earlier, we refer to integer types as "int" and types of numbers with decimal points as "float."

The result of an operation between two int values will be an int, and in cases where two float values are involved or when int and float types are mixed in an operation, the result becomes a float value.

Represented in a table, it looks like this:

[Result of Operation between A and B]
A: int A: float
B: int Result: int Result: float
B: float Result: float Result: float

Similarly, operations involving at least one "complex" type value will result in a complex type. In other words, when you add complex numbers to the table above, it becomes like this:

[Result of Operation between A and B]
A: int or float A: complex
B: int or float Result: The table above Result: complex
B: complex Result: complex Result: complex

(Important) Be Cautious with Integer Division

Now, the rules we've seen above require some caution when it comes to division.

In general, the results of addition, subtraction, and multiplication between integers are also integers, which is mathematically sound and doesn't pose any issues. However, the same cannot be said for division between integers. In VCSSL, as in many programming languages, dividing one integer by another always returns an integer result. This can lead to tricky pitfalls until you get used to it.

For example, try this:

- Execution Result -

0

When you execute this program, it displays an integer "0."

Mathematically, it should be "0.5." This is a common behavior among many programming languages.

To obtain a result with a decimal point, cast operands to a float-type like this:

- Execution Result -

0.5

When you execute this, it correctly displays "0.5" as the result.

If you are performing operations directly on values and not variables, you can also add ".0" at their tail to make them float-type values:

- Execution Result -

0.5

This will also correctly display "0.5." However, note that adding ".0" to variables does not convert their types to float.

Specifying the Order of Operations

When performing multiple operations, the order of the operations becomes an issue. For example, consider the following case:

- Execution Result -

7

In such cases, multiplication and division are calculated before addition and subtraction. This means the value of 2*3=6 is added to 1, resulting in an output of 7.

If you want to calculate 1+2 first, enclose it with parentheses ( ) like this:

- Execution Result -

9

This way, 1+2=3 is calculated first, and then it is multiplied by 3, resulting in the output of 9.

Precedence of Arithmetic Operators

In expressions containing multiple operators, those with higher precedence are processed first. The precedence is set in the following order:

- Precedence or operators -

 Exponentiation > Remainder = Division = Multiplication > Subtraction = Addition

* Please note: In this notation, operators on the left side have higher precedence, meaning they are evaluated first. This use of the '>' symbol indicates precedence levels, not numerical values.

This means that multiplication and division have higher precedence and are processed before addition and subtraction. Also, when operators of the same precedence, such as multiplication and division, are adjacent, they are processed from left to right.

String Concatenation

Also, for the string type, the behaviour of operators differs significantly from other data-types. First, operators other than addition (+) cannot be used for the string type. Moreover, when performing addition between a string type operand and an operand of another data-type, all operands are converted to the string type before the operation.

For example, try executing the following example.

- Execution Result -

12

When you execute this, it displays "12." This happens because the operation is treated as concatenating the string '1' with the string '2'."

Acknowledgement: We greatly appreciate the cooperation of two ChatGPT AIs in translating this article!
» How we translated this article


Sponsored Link



Japanese English
Index
News

Software Updates: Command Expansion in RINEARN Graph, and English Support in VCSSL
2024/02/05 - We updated our apps. This updates include "Enhancing the Command-Line Features of RINEARN Graph" and "Adding English Support to VCSSL." Delves into each of them!

Inside the Repetitive Execution Speedup Impremented in Vnano Ver.1.1
2024/01/17 - Delves into the update in Vnano 1.1 from a developer's viewpoint, providing detailed insights into the specific improvements made to the internal structure of the script engine.

Scripting Engine Vnano Ver.1.1 Released: Dramatic Speed Improvement for Repetitive Executions of the Same Content
2023/12/22 - Released the Vnano script engine Ver.1.1. In this version, we've made significant enhancements in processing speed by reducing the overhead of handling requests. Explains the details.

Updated Contents
Circular Wave Animation

Draws the circular wave as 3D animation, under the specified wave parameters.
2022/12/14
Sine Wave Animation

Draws the sine wave as animation, under the specified wave parameters.
2022/11/26
Tool For Converting Units of Angles: Degrees and Radians

A GUI tool for converting the angle in degrees into radians, or radians into degrees.
2022/11/22
Connector Fatal Exception - Specification
The unchecked exception thrown when errors have occurred, caused by incorrect implementations (might be bugs).
2022/09/26
Connector Exception - Specification
The checked exception thrown when errors have occurred, cause by expected normal problems.
2022/09/26