# GAMBAS BOOK 3.19.3

08.10.2022
k8:k8.2:start

## 8.2 Operations and Comparisons for Numbers

In the case of operations with numbers as operands, on the one hand, the types of operands must be taken into account and, on the other hand, the type of the result of the operation. When dividing two real numbers, the quotient is always of the float type. The two numbers iNumber1 and iNumber2, on the other hand, must be integers in operation iNumber1 MOD iNumber2.

## 8.2.1 Arithmetic operators

Note that there are three different operations for dividing two numbers with different operators (/, MOD ≡ %, DIV ≡ \).

Operation Description
-Number Calculates the opposite number, where zero is the opposite of itself; the sign is inverted.
Number1 + number2 Two numbers are added.
Number1 - Number2 Two numbers are subtracted.
Number1 * Number2 Two numbers are multiplied.
Basis ^ ExponentThe power value to base b is calculated with the exponent e for real numbers b and e. An example is b^e = 4^3 = 64.
Number1 / number2 Two numbers are divided. The quotient is of the type Float. A DivisionByZero error occurs when the divisor is zero.
iNumber1 MOD iNumber2 or iNumber1 % iNumber2Calculates the rest of the quotient of two integers. The result of the operation is an integer. A DivisionByZero error occurs when the divisor is zero.
iNumber1 DIV iNumber2 or iNumber1 iNumber2Calculates the quotient of two integers - rounded down or the integer part of the quotient iNumber1 / iNumber2. the result of the operation is an integer. A DivisionByZero error occurs when the divisor is zero.

Table 8.2.1.1: Arithmetic operators

In many cases, not only the basic operations described above are used, but also complex mathematical calculations are carried out. For complex arithmetic operations, a distinction is made between priority and associativity if at least 2 operators are present in a calculation instruction. Priority describes the order in which mathematical terms are linked to each other and associativity is the priority if there are at least two operators of equal rank. Chapter 8.5' Ranking of Operations' provides detailed information on the associativity and priority of operations. Interesting is the operator ^, which calculates the power value to a base b and the exponent e with b, e?

```2^3 =  8
2^(-3) =  0,125
2^3.44 =  10,8528346195814
(-2)^3 =  -8
-2^3 =  -8
(-2)^(-3) =  -0,125
-2^-3 =  -0,125
3.2^(-0.77) =  0,40835183987713
3.2^-0.77 =  0,40835183987713
SQR(5)^CBR(-22.7) =  0,10243569625776
1^0.88 =  1
0^3 =  0
3.4567^0 =  1```

## 8.2.1.1.1 Operator MOD or %

The MOD or % operator is used to calculate the remainder of the division of two integers. In the integer division with remainder there are different views on how to calculate the integer quotient and the remainder with different number ranges. The illustration {Z, N} R* assigns division R* of division Z/N to each pair of whole numbers. Since the figure is unique, it is a function with the function name modulo or abbreviated mod or %. The modulo function is used in Gambas as operator MOD in mathematical calculations. By definition, the following applies: z MOD n = z - |_z/n_| * n with the symbol |_z/n_| as a rounding function or Gauss bracket. For a real number q, |_q_| is the largest integer smaller than or equal to q. These preliminary remarks are necessary because gambas do NOT follow this so-called mathematical approach, as you can read in the help for gambas. However, if you prefer the mathematical approach, the following function will help you:

```Public Function myMOD(iA As Integer, iM As Integer) As Integer
Return iA - (Int(iA / iM)) * iM
End```

The following section explains how to determine the difference between the mathematical approach and the definition of MOD in Gambas:

```Print "11 MOD 4 = ";; 11 Mod 4
Print "11 myMOD 4 = ";; myMOD(11, 4)
Print "11 MOD -4 = ";; 11 Mod -4
Print "11 myMOD -4 = ";; myMOD(11, -4)
Print "-11 MOD 4 = ";; -11 Mod 4
Print "-11 myMOD 4 = ";; myMOD(-11, 4)
Print "-11 MOD -4 = ";; -11 Mod -4
Print "-11 myMOD -4 = ";; myMOD(-11, -4)

11 MOD 4 =  3
11 myMOD 4 =  3

11 MOD -4 =  3
11 myMOD -4 =  -1

-11 MOD 4 =  -3
-11 myMOD 4 =  1

-11 MOD -4 =  -3
-11 myMOD -4 =  -3```

## 8.2.1.2 Operator DIV or \

Although in the definition for the operator DIV “Calculations the quotient of two integer numbers, rounding down. Calculates the quotient from two integers and rounds it down”, there are errors in calculations with the operator DIV, because the above definition is not consistently implemented. You can avoid errors by using the myDIV (..) function:

```Public Function myDIV(iA As Integer, iM As Integer) As Integer
Return Int(iA / iM)
End ' myDIV(iA As Integer, iM As Integer)```
```20:7 =  2,85714285714286
20 DIV 7 =  2
20 myDIV 7 =  2

20:(-7) =  -2,85714285714286
20 DIV -7 =  -2
20 myDIV -7 =  -3

(-20):7 =  -2,85714285714286
-20 DIV 7 =  -2
-20 myDIV 7 =  -3

(-20):(-7) =  2,85714285714286
-20 DIV -7 =  2
-20 myDIV -7 =  2```

The following source code shows the use of the operators DIV and MOD:

```[1] Public Sub btnM2H_Click()
[2]   Dim iTotalMinutes, iHours, iRemainingMinutes As Integer
[3]   Dim sComment As String
[4]
[5]   iTotalMinutes = 444
[6]   iHours = iTotalMinutes Div 60
[7]   iRemainingMinutes = iTotalMinutes Mod 60
[8]
[9]   If iRemainingMinutes Mod 2 = 0 Then
[10]      sComment = "Remainingminutes = " & Str(iRemainingMinutes) & " ---> even number"
[11]   Else
[12]      sComment = "Remainingminutes = " & Str(iRemainingMinutes) & " ---> odd number"
[13]   Endif
[14]   Print "Hours = ";; iHours
[15]   Print "RemainingMinutes = ";; iRemainingMinutes
[16]   Print sComment
[17]
[18] End ' btnM2H_Click()```

Especially the comparison in line 9 is often used to determine if an integer is even (number mod 2 = 0) or odd. This is also used in the next section of the source code so that the lines in a GridView are coloured differently in order to achieve better readability. Attention: For the 1st displayed row Row = 0 applies to the GridView:

```[1] Public Sub TableView1_Data(Row As Integer, Column As Integer)
[2]   TableView1.Data.Text = aMatrix[Row][Column]
[3]   If Row MOD 2 = 0 Then
[4]      TableView1.Data.Background = Colour.RGB(224, 224, 224) ' light grey
[5]   Endif ' MOD 2 = 0?
[6] End ' TableView1_Data(..)```

Figure 8.2.1.2.1 Alternative colouring of the rows in a grid view

## 8.2.2 Comparison operators for numbers

There are 6 comparison operators available for comparing two numbers:

ComparisonDescription
Number1 = Number2 Returns TRUE if the two numbers are equal.
Number 1 <> Number Returns TRUE if the two numbers are not equal.
Number1 < Number2 Returns TRUE if the number1 is less than number 2.
Number1 > Number2 Returns TRUE if the number1 is greater than number 2.
Number1 <= Number2 Returns TRUE if the number1 is less than or equal to number 2.
Number1 >= Number2 Returns TRUE if the number1 is greater than or equal to number 2.

Table 8.2.2.1: Comparison operators for numbers

If the result of a comparison of 2 numbers is assigned to an integer variable instead of a boolean variable, the result is either -1 (True) or 0 (False).

Comparisons are to be considered with particular care in which the equality of two real numbers is to be determined or whether a variable of the type float has the value zero. Good experiences have been made with the following instructions, for example, in the calculation of value tables. First, the system checks whether the function value y = f(x) can be calculated for an argument x. The argument is then set to zero if the argument is sufficiently small. Finally, the function value y = f(x) is tested. In a similar way, you can also proceed with the values for x and y to detect overflows, for example, or to reduce (sufficiently) large values for the argument or the function value to task-adequate values:

```[1] IF aValuePair.Valid = True THEN
[2]    IF Abs(x) < 1E-8 THEN aValuePair.x = 0
[3]    IF Abs(aValuePair.y) < 1E-6 THEN aValuePair.y = 0
[4] ENDIF```

This is the content of the class ValuePair.class:

```' Gambas class file

' This class is a data structure without its own methods.
' It represents, among other things, a pair of xy values. The valid variable must be
' and indicates whether the xy value pair is valid.
' Tag is a universally applicable variable. It is used by the parser class to save
' the error message for invalid objects.

Public x As Float
Public y As Float

Public Valid As Boolean
Public Tag As Variant```
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.