1 Introduction

Arithmetic is the part of mathematics that studies numbers and numerical operations. In this section, we explain different arithmetic operations that XSQL Script supports. The manner of expression for these operations resembles Polish notation, contiguous operators bounded by the start and end markers of the operation.

The XSQL Script language supports several arithmetic operators for floating-point numbers and integers. For data types such as date, time or interval, addition or subtraction operations are supported. And for string data types, addition produces the concatenation of two strings.

Arithmetic Operators
Operator Equivalent JAVA Class Description Applicable to
<add> + BINARY / MULTIPLE Sum number, string, date, time, timestamp
<sub> - BINARY / MULTIPLE Subtraction number, date, time, timestamp
<mul> * BINARY / MULTIPLE Multiplication number
<div> / BINARY / MULTIPLE Division number
<mod> % BINARY / MULTIPLE Remainder number
<exp> ^ BINARY / MULTIPLE Power number
<neg> -(number) UNARIO Negation (change of sign) number

In XSQL arithmetic evaluations are very flexible in terms of the data types of the operands. All arithmetic operations are binary or multiple, and therefore require two or more operands. That is, in XSQL the sum operation can contain two or more values. The data type resulting from an arithmetic operation is determined by the data types of the operands.

The decision table for the application of relational operators is shown below. The order in which cases are listed is followed by the evaluator. Relationships not included in the table are not supported. For example, it is not possible to add two arrays or a number to an array.

Application and Results of Arithmetic Operations
Type 1 Type 2 Result Operations Notes
null any null add, sub, mul, div, mod, exp If either of the two operators is null, the result is null.
string any string add If an operator is a string, the addition operation concatenates the first result of converting the second string.
smallint smallint integer add, sub, mul, div, mod, exp If both are integer or smallint, the result is an integer (except in expressions that can return a long type).
decimal number decimal add, sub, mul, div, mod, exp If either one is a decimal and the other is numeric, the result is a decimal.
number number double add, sub, mul, div, mod, exp Two numbers whose types are not included in the above cases are treated as double.
date date integer (days) add, sub The addition or subtraction of two date types results in an integer number of days between the dates.
date timestamp long (seconds) add, sub The addition or subtraction of a time or timestamp to a date is a long type indicating the number of seconds.
date number (days) date add, sub The addition or subtraction of a number to a date returns a date applying the operation in days.
date dateunits (day, month, year) date add, sub The addition or subtraction of date units in a range day, month or year range returns a date.
date dateunits (hour, minute, second) timestamp add, sub The addition or subtraction of date units in the time range returns a timestamp.
time dateunits (any) time add, sub The addition or subtraction of date units to a time of day returns a time of day.
timestamp dateunits (any) timestamp add, sub The addition or subtraction of date units to an interval returns an interval.

Additionally, the unary arithmetic operator <neg> is available.

Application and Results of Unary Conditional Operations
Type 1 Result Operations Notes
number -(number) neg The NEG operation will reverse the sign.

2 <add> function

Sum n operators

<add>
    <value_1> !
    <value_2> !
</add>

exception

invalid null expression

no arguments specified

Example

Add the content of 1 integer-type variable to an integer constant expression.

Copy
<xsql-script name='add_sample1'>
    <body>
        <set name='a' type='integer' value='1' />      --> a = 1
        <println><add><a/>1</add></println>            --> a + 1 = 1 + 1 = 2
    </body>
</xsql-script>
Example

Add the content of 1 integer-type variable (same operand repeated 4 times).

Copy
<xsql-script name='add_sample2'>
    <body>
        <set name='a' type='integer' value='1' />      --> a = 1
        <println><add><a/><a/><a/><a/></add></println> --> a + a + a + a = 1 + 1 + 1 + 1 = 4
    </body>
</xsql-script>
Example

Add the content of 1 integer-type variable to the value of another variable of the same type.

Copy
<xsql-script name='add_sample3'>
    <body>
        <set name='a' type='integer' value='1' />      --> a = 1
        <set name='b' type='integer' value='4' />      --> b = 4
        <println><add><a/><b/></add></println>         --> a + b = 1 + 4 = 5
    </body>
</xsql-script>
Example

Add the content of 1 integer-type variable to the value of another decimal-type variable.

Copy
<xsql-script name='add_sample4'>
    <body>
        <set name='a' type='integer' value='1'   />    --> a = 1
        <set name='c' type='decimal' value='6.2' />    --> c = 6.2
        <println><add><a/><c/></add></println>         --> a + c = 1 + 6.2 = 7.2
    </body>
</xsql-script>

3 <sub> function

Subtract n operands

<sub>
    <value_1> !
    <value_2> !
</sub>

exceptions

invalid null expression

no arguments specified

Example

Subtract the contents of 1 integer-type variable from an integer constant expression.

Copy
<xsql-script name='sub_sample1'>
    <body>
        <set name='a' type='integer' value='1' />      --> a = 1
        <println><sub><a/>1</sub></println>            --> a - 1 = 1 - 1 = 0
    </body>
</xsql-script>
Example

Subtraction of the contents of 1 integer-type variable (same operand repeated 4 times).

Copy
<xsql-script name='sub_sample2'>
    <body>
        <set name='a' type='integer' value='1' />      --> a = 1
        <println><sub><a/><a/><a/><a/></sub></println> --> a - a - a - a = 1 - 1 - 1 - 1 = -2
    </body>
</xsql-script>
Example

Subtract the content of 1 integer-type variable from the value of another variable of the same type.

Copy
<xsql-script name='sub_sample3'>
    <body>
        <set name='a' type='integer' value='1' />      --> a = 1
        <set name='b' type='integer' value='4' />      --> b = 4
        <println><sub><b/><a/></sub></println>         --> b - a = 4 - 1 = 3
    </body>
</xsql-script>
Example

Subtract the content of 1 integer-type variable from the value of another decimal-type variable.

Copy
<xsql-script name='sub_sample4'>
    <body>
        <set name='b' type='integer' value='4'   />    --> b = 4
        <set name='c' type='decimal' value='6.2' />    --> c = 6.2
        <println><sub><c/><b/></sub></println>         --> c - b = 6.2 - 4 = 2.2
    </body>
</xsql-script>

4 <mul > function

Multiply n factors

<mul>
    <factor_1> !
    <factor_2> !
</mul>

exceptions

at least 2 arguments required

illegal operator

illegal object types

Example

Multiply the contents of 1 integer variable by a whole constant expression.

Copy
<xsql-script name='mul_sample1'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <println><mul><a/>1</mul></println>            --> a * 1 = 2 * 1 = 2
    </body>
</xsql-script>
Example

Multiply the contents of 1 integer variable by itself 4 times.

Copy
<xsql-script name='mul_sample2'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <println><mul><a/><a/><a/><a/></mul></println> --> a * a * a * a = 2 * 2 * 2 * 2 = 16
    </body>
</xsql-script>
Example

Multiply the contents of 1 integer variable by another variable of the same type.

Copy
<xsql-script name='mul_sample3'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <set name='b' type='integer' value='3' />      --> b = 3
        <println><mul><a/><b/></mul></println>         --> a * b = 2 * 3 = 6
    </body>
</xsql-script>
Example

Multiply the contents of 1 integer variable by another decimal variable.

Copy
<xsql-script name='mul_sample4'>
    <body>
        <set name='b' type='integer' value='3'   />    --> b = 3
        <set name='c' type='decimal' value='6.2' />    --> c = 6.2
        <println><mul><b/><c/></mul></println>         --> b * c = 3 * 6.2 = 18.6
    </body>
</xsql-script>

5 <div> function

Get the quotient of division. Unlike multiplication, this operation must always consist of two operands: the dividend and the divisor.

<div>
    <dividendo> !
    <divisor> !
</div>

exceptions

at least 2 arguments required

illegal operator

illegal object types

Example

Divide the content of 1 integer variable by an integer constant expression.

Copy
<xsql-script name='div_sample1'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <println><div><a/>1</div></println>            --> a / 1 = 2 / 1 = 2
    </body>
</xsql-script>
Example

Divide the content of 1 integer variable by a variable of the same type.

Copy
<xsql-script name='div_sample2'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <set name='b' type='integer' value='3' />      --> a = 3
        <println><div><b/><a/></div></println>         --> b / a = 3 / 2 = 1
    </body>
</xsql-script>
Example

Divide the content of 1 integer variable by a decimal-type variable.

Copy
<xsql-script name='div_sample3'>
    <body>
        <set name='b' type='integer' value='3'   />    --> b = 3
        <set name='c' type='decimal' value='6.3' />    --> c = 6.3
        <println><div><c/><b/></div></println>         --> c / b = 6.3 / 3 = 2.1
    </body>
</xsql-script>

6 <mod> function

Get the remainder of a division. Dividend and divisor must be indicated between the start and end mark of the <mod> tag.

<mod>
    <dividend> !
    <divisor> !
</mod>

exceptions

at least 2 arguments required

illegal operator

illegal object types

Example
Copy
<xsql-script name='mod_sample1'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <println><mod><a/>2</mod></println>            --> a % 2 = 2 % 2 = 0
    </body>
</xsql-script>
Example
Copy
<xsql-script name='mod_sample2'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <set name='c' type='decimal' value='8' />      --> c = 8
        <println><mod><c/><a/></mod></println>         --> c % a = 8 % 2 = 0
    </body>
</xsql-script>
Example
Copy
<xsql-script name='mod_sample3'>
    <body>
        <set name='a' type='integer' value='2' />      --> a = 2
        <set name='b' type='integer' value='3' />      --> b = 3
        <println><mod><b/><a/></mod></println>         --> b % a = 3 % 2 = 1
    </body>
</xsql-script>

The concept behind division is that dividing one number (dividend) by another (divisor) returns a number ( quotient) that, when multiplied by the divisor, becomes the dividend. When both operands are integers, as in the example cases, the remainder is 0 when the dividend is multiple or equal to the divisor.

7 <exp> function

With this function you can perform exponential operations. The base and the exponent must be indicated as operands.

<exp>
    <base> !
    <potencia> !
</exp>

exceptions

at least 2 arguments required

illegal operator

illegal object types

Example
Copy
<xsql-script name='exp_sample1'>
    <body>
        <set name='a' type='integer' value='2' />  --> a = 2
        <println><exp><a/>0</exp></println>        --> a exp 0 = 2 exp 0 = 1.0
    </body>
</xsql-script>

Remarks

This example implements one of the properties of this type of operation: exponent power 0 is equal to 1, as long as the base is different from 0.

Example
Copy
<xsql-script name='exp_sample2'>
    <body>
        <set name='a' type='integer' value='2' />  --> a = 2
        <println><exp><a/>1</exp></println>        --> a exp 1 = 2 exp 1 = 2.0
    </body>
</xsql-script>
Example
Copy
<xsql-script name='exp_sample3'>
    <body>
        <set name='a' type='integer' value='2' />  --> a = 2
        <set name='b' type='integer' value='3' />  --> b = 3
        <println><exp><a/><b/></exp></println>     --> b exp a = 2 exp 3 = 2 * 2 * 2 = 8.0
    </body>
</xsql-script>
Example
Copy
<xsql-script name='exp_sample4'>
    <body>
        <set name='a' type='integer' value='2' />  --> a = 2
        <set name='b' type='integer' value='3' />  --> b = 3
        <set name='c' type='integer' value='1' />  --> c = 1
        <println><exp><a/><b/><c/></exp></println> --> (a exp b) exp c = (2 exp 3) exp 1 = 8 exp 1 = 8
    </body>
</xsql-script>

8 <neg> function

Returns the negative value of the number, that is, returns numerical value with the opposite sign. If the value is NULL, it returns NULL. If a decimal type, the BigDecimal class negate() function is used, but a negative sign is simply applied in front of the value, to obtain the opposite sign.

<neg>
    <value> !
</neg>

exceptions

Returns NULL if the value is NULL. Reverses the sign of value.

neg not valid for type

Example
Copy
<xsql-script name='sample'>
    <body>
        <!-- neg null: null -->
        <println><neg><null /></neg></println>

        <!-- neg 98.9: -98.0 -->
        <println><neg>98.9</neg></println>

    </body>
</xsql-script>