# 1 Arithmetic operators

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.

The addition operator (+) produces the sum of numeric operands.The two numbers can be literals. In the example, the sum of two numbers is carried out to be stored in variables.

Syntax Value1 + Value2 + ... Value(n)
Return The sum of numbers.

In the example, add the content of 1 integer-type variable to the value of another variable of the same type.

Copy
<xsql-script>
<body>
<set name='m_total'>
<number>20</number>
<number>5</number>
</set>

<println>Total : [<m_total/>]</println>
</body>
</xsql-script>
Copy
var mIntTotal = 20 + 5;

console.log(Total : [${mIntTotal}]); or variables: Copy <xsql-script> <body> <set name='m_value1'>5</set> <set name='m_value2'>10</set> <set name='m_total'> <add> <m_value1/> <m_value2/> </add> </set> <println>Total : [<m_total/>]</println> </body> </xsql-script> Copy var mIntValue1 = 5; var mIntValue2 = 10; var mIntTotal = mIntValue1 + mIntValue2; console.log(Total : [${mIntTotal}]);

## 1.2 Subtraction

The subtraction operator (-) subtracts the two operands, producing their difference.

Subtraction OPERATOR
Syntax Value1 - Value2 - ... Value(n)
Return Subtraction of numbers.

In the example, subtract the content of 1 integer-type variable from the value of another variable of the same type.

Copy
<xsql-script>
<body>
<set name='m_value1'>40</set>
<set name='m_value2'>13</set>

<set name='m_total'>
<sub>
<m_value1/>
<m_value2/>
</sub>
</set>

<println>Total : [<m_total/>]</println>
</body>
</xsql-script>
Copy
var mIntValue1 = 40;
var mIntValue2 = 13;

var mIntTotal = mIntValue1 - mIntValue2;

## 1.4 Division

The division operator (/) produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.

Division OPERATOR
Syntax Value1 / Value2
Return The quotient of its operands.

In the example, divide the content of 1 integer variable by a variable of the same type.

Copy
<xsql-script>
<body>
<set name='m_value1'>81</set>
<set name='m_value2'>3</set>

<set name='m_total'>
<div>
<m_value1/>
<m_value2/>
</div>
</set>

<println>Total : [<m_total/>]</println>
</body>
</xsql-script>
Copy
var mIntValue1 = 81;
var mIntValue2 = 3;

var mIntTotal = mIntValue1 / mIntValue2;

## 1.6 Increment

The increment operator (++) increments (adds one to) its operand and returns a value.

Increment OPERATOR
Syntax Value++
Return A number increment in one.
Copy
<xsql-script>
<body>
<set name='m_value'>5</set>

<set name='m_value'>
<m_value/>
1
</set>

<println>Value : [<m_value/>]</println>
</body>
</xsql-script>
Copy
var mIntValue = 5;

mIntValue++;

console.log(Value : [${mIntValue}]); Also can mix increment operator with other operations like multiplication for example but it's important to understand the meaning of the position where it writes the variable that will be increment and how impact in the final result. To get a better understanding of how it works let's do an example in server- side script. In this example, the increment operator (++) is before the variable. Copy var mIntValue = 5; var mIntResult = ++mIntValue * 2; console.log(Result of the operation: [${mIntResult}]);

console.log(Final value of variable mIntValue : [${mIntValue}]); In this example, the increment operator (++) is after the variable. Copy var mIntValue = 5; var mIntTotal = mIntValue++ * 2; console.log(Result of the operation : [${mIntTotal}]);

console.log(Final value of variable mIntValue : [${mIntValue}]); As a result have that if put the increment operator (++) before the variable, first increment the variable in one and then do the operation, impacting in the final result. However if the position of the increment operator (++) after the variable ,first do the operation and then increase in one the variable ## 1.7 Decrement The decrement operator (--) decrements (subtracts one from) its operand and returns a value. Decrement OPERATOR Syntax Value-- Return A number decrement in one. Copy <xsql-script> <body> <set name='m_value'>20</set> <set name='m_value'> <sub> <m_value/> 1 </sub> </set> <println>Value : [<m_value/>]</println> </body> </xsql-script> Copy var mIntValue = 20; mIntValue--; console.log(Value : [${mIntValue}]);

Also can mix decrement operator with other operations like multiplication for example but it's important to understand the meaning of the position where it writes the variable that will be decrement and how impact in the final result.

To get a better understanding of how it works let's do an example in server- side script.

In this example, the decrement operator (--) is before the variable.

Copy
var mIntValue = 19;

var mIntResult = --mIntValue * 2;

console.log(Result of the operation: [${mIntResult}]); console.log(Final value of variable mIntValue : [${mIntValue}]);

In this example, the decrement operator (--) is after the variable.

Copy
var mIntValue = 19;

var mIntTotal = mIntValue-- * 2;

console.log(Result of the operation : [${mIntTotal}]); console.log(Final value of variable mIntValue : [${mIntValue}]);

As a result have that if put the decrement operator (--) before the variable, first decrement the variable in one and then do the operation, impacting in the final result. However if the position of the decrement operator (--) after the variable ,first do the operation and then decrease in one the variable

## 1.8 Negation

The unary negation operator (-) precedes its operand and negates it. Returns the negative value of the number, that is, returns numerical value with the opposite sign.

Negation
Operator Xsql <neg>
Syntax -Value
Return The number multiplied by -1 (change of sign).
Copy
<xsql-script name='Negation_sample1'>
<body>
<set name='m_value'>17</set>

<set name='m_value_negative'>
<neg>
<m_value/>
</neg>
</set>

<println>Value negative : [<m_value_negative/>]</println>
</body>
</xsql-script>
Copy
var mIntValue = 17;

var mIntNegative = - mIntValue;

console.log(Value negative : [${mIntNegative}]); # 2 Mathematical tasks ## 2.1 Math Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object. Remenber that Math works with the Number type. It doesn't work with BigInt. ### 2.1.1 E The Math.E property represents the base of natural logarithms, e, approximately 2.718. e PROPERTY Operator Xsql <math.e> Syntax Math.E Return Approximately 2.718281828459045 Copy <xsql-script> <body> <println>e value : [<math.e />]</println> </body> </xsql-script> Copy console.log(E value : [${Math.E}]);

Math.E returns E (2.718281828459045) with a precision of 16 decimals.

### 2.1.2 Ln2

The Math.LN2 property represents the natural logarithm of 2, which is approximately 0.69314.

ln2 PROPERTY
Syntax Math.LN2
Return Approximately 0.6931471805599453
Copy
<xsql-script>
<body>
<set name='m_value'>2</set>

<set name='m_ln2'>
<math.log>
<m_value/>
</math.log>
</set>

<println>Ln(<m_value/>) = [<m_ln2 />]</println>
</body>
</xsql-script>
Copy

### 2.1.4 Log2e

The Math.LOG2E property represents the base 2 logarithm of e, which is approximately 1.44269.

log2e PROPERTY
Syntax Math.LOG2E
Return Approximately 1.4426950408889634
Copy

### 2.1.6 Pi

The Math.PI property represents the ratio of the circumference of a circle to its diameter, approximately 3.14159.

pi PROPERTY
Operator Xsql <math.pi>
Syntax Math.PI
Return Approximately 3.141592653589793
Copy
<xsql-script>
<body>
<println>Pi value : [<math.pi />]</println>
</body>
</xsql-script>
Copy

### 2.1.8 Square root of 2

The Math.SQRT2 property represents the square root of 2, approximately 1.41421.

sqrt2 PROPERTY
Syntax Math.SQRT2
Return Approximately 1.4142135623730951
Copy
<xsql-script>
<body>
<set name='m_value'>2</set>

<set name='m_sqrt2'>
<math.sqrt>
<m_value/>
</math.sqrt>
</set>

<println>Square root of <m_value /> : [<m_sqrt2/>]</println>
</body>
</xsql-script>
Copy

### 2.1.10 Acos

The Math.acos() function returns the arccosine (in radians) of a number.

acos() METHOD
Operator Xsql <math.acos>
Syntax Math.acos(x)
Parameters x: a number.
Return Arccosine of a number in radians.
Copy
<xsql-script>
<body>

<set name='m_hypotenuse'>2</set>

<set name='m_arccosine'>
<math.acos>
</math.acos>
</set>

<println>Arccosine (in radians) : [<m_arccosine />]</println>

</body>
</xsql-script>
Copy
var mIntAdjacent = 1;
var mIntHypotenuse = 2;

### 2.1.12 Atan

The Math.atan() function returns the arctangent (in radians) of a number.

atan() METHOD
Operator Xsql <math.atan>
Syntax Math.atan(x)
Parameters x: a number.
Return Arctangent of a number in radians.
Copy
<xsql-script>
<body>
<set name='m_opposite'>5</set>

<set name='m_arctangent'>
<math.atan>
</math.atan>
</set>

<println>Arctangent (in radians) : [<m_arctangent />]</println>
</body>
</xsql-script>
Copy
var mIntOpposite = 5;

### 2.1.14 Cos

The Math.cos() static function returns the cosine of the specified angle, which must be specified in radians. This value is length adjacent length hypotenuse.

cos() METHOD
Operator Xsql <math.cos>
Syntax Math.cos(x)
Parameters x: a number in radians.
Return Cosine of the specified angle.
Copy
<xsql-script>
<body>
<set name='m_angle'>1</set>

<set name='m_cos'>
<math.cos>
<m_angle/>
</math.cos>
</set>

<println>Cos(<m_angle />) = [<m_cos />]</println>
</body>
</xsql-script>
Copy
var mIntAngle = 1;

var mBcCos = Math.cos(mIntAngle);

console.log(Cos(${mIntAngle}) = [${mBcCos}]);

### 2.1.15 Exponentiation

The exponentiation operator (Math.pow) returns the result of raising the first operand to the power of the second operand.

pow() METHOD
Operator Xsql <exp>
Syntax Math.pow(x,y)
Parameters

x: base number

y: exponent number.

Return Exponentiation.
Copy
<xsql-script>
<body>
<set name='m_base'>2</set>
<set name='m_exponent'>8</set>

<set name='m_total'>
<exp>
<m_base/>
<m_exponent/>
</exp>
</set>

<println>Total : [<m_total/>]</println>
</body>
</xsql-script>
Copy
var mIntBase = 2;
var mIntExponent = 8;

var mIntTotal = Math.pow(mIntBase, mIntExponent);

### 2.1.17 Log

The Math.log() function returns the natural logarithm (base e) of a number.

log() METHOD
Operator Xsql <math.log>
Syntax Math.log(x)
Parameters x: a number.
Return Natural logarithm (base e) of a number.
Copy
<xsql-script>
<body>
<set name='m_value'>10</set>

<set name='m_logarithm'>
<math.log>
<m_value/>
</math.log>
</set>

<println>Ln(<m_value/>) = [<m_logarithm />].</println>
</body>
</xsql-script>
Copy
var mIntValue = 10;

var mBcLogarithm = Math.log(mIntValue);

console.log(Ln(${mIntValue}) = [${mBcLogarithm}]);

### 2.1.18 Max

The Math.max() function returns the largest of the zero or more numbers given as input parameters.

max() METHOD
Operator Xsql <math.max>
Syntax Math.max(Value1, Value2, ...)
Parameters Zero or more numbers among.
Return Largest of the arguments received.
Copy
<xsql-script>
<body>
<set name='m_value1'>3</set>
<set name='m_value2'>6</set>

<set name='m_max_value'>
<math.max>
<m_value1/>
<m_value2/>
</math.max>
</set>

<println>Max value : [<m_max_value/>]</println>
</body>
</xsql-script>
Copy
var mIntValue1 = 3;
var mIntValue2 = 6;

var mIntMaxValue = Math.max(mIntValue1, mIntValue2);

### 2.1.20 Random

The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1).

random() METHOD
Operator Xsql <math.random>
Syntax Math.random()
Return Random number.
Copy
<xsql-script>
<body>
<set name='m_value'><math.random/></set>

<println>Random value : [<m_value/>]</println>
</body>
</xsql-script>
Copy
var mBcValue = Math.random();

### 2.1.22 Sin

The Math.sin() function returns the sine of a number.

sin() METHOD
Operator Xsql <math.sin>
Syntax Math.sin(x)
Parameters x: a number.
Return Sine of a number.
Copy
<xsql-script>
<body>
<set name='m_angle'>1</set>

<set name='m_sin'>
<math.sin>
<m_angle/>
</math.sin>
</set>

<println>Sin(<m_angle />) = [<m_sin />]</println>
</body>
</xsql-script>
Copy
var mIntAngle = 1;

var mBcSin = Math.sin(mIntAngle);

console.log(Sin(${mIntAngle}) = [${mBcSin}]);

### 2.1.23 Square root

The Math.sqrt() function returns the square root of a number.

sqrt() METHOD
Operator Xsql <math.sqrt>
Syntax Math.sqrt(x)
Parameters x: a number.
Return Square root.
Copy
<xsql-script>
<body>
<set name='m_value'>64</set>

<set name='m_square_root'>
<math.sqrt>
<m_value/>
</math.sqrt>
</set>

<println>Square root : [<m_square_root/>]</println>
</body>
</xsql-script>
Copy
var mIntValue = 64;

var mIntSquareRoot = Math.sqrt(mIntValue);

### 2.2.2 MaxValue

The Number.MAX_VALUE property returns the largest number possible in server- side script.

max_value PROPERTY
Syntax Number.MAX_VALUE
Return Approximately 1.7976931348623157e+308.

In case that it use a specific value or number object like mynumber.MAX_VALUE, it will return undefined.

Copy

### 2.2.4 toFixed

The toFixed() method converts a number into a string, rounding to a specified number of decimals.

toFixed() METHOD
Syntax Value.toFixed(x)
Parameters x: number of decimals to round after de point.
Return A number formated.
Copy
<xsql-script>
<body>
<set name='m_value' type='decimal'>16.1784</set>

<set name='m_value_to_round' type='integer'>2</set>

<set name='m_value_tofixed' type='decimal'>
<math.round>
<m_value/>
<m_value_to_round/>
</math.round>
</set>

<println>m_value_tofixed = <m_value_tofixed /></println>
</body>
</xsql-script>
Copy
var mBcValue1 = 16.1784;

### 2.2.6 toString

The toString() method converts a number to a string.

toString() METHOD
Syntax Value.toString()
Return A string, representing a number.
Copy
<xsql-script name='tostring_sample1'>
<body>
<set name='m_value_integer' type="integer">5</set>

<set name='m_value_to_string'>
<string>
<m_value_integer />
</string>
</set>

<println>m_value_integer [<m_value_integer />] : <variable.typeof><m_value_integer /></variable.typeof></println>

<println>m_value_to_string [<m_value_to_string />] : <variable.typeof><m_value_to_string /></variable.typeof></println>
</body>
</xsql-script>
Copy
var mIntValue = 12;

var mStrValue = mIntValue.toString();

# 3 BCMath Arbitrary Precision Mathematics

Floating-point numbers are represented as binary (base 2) fractions. Regrettably, most decimal fractions cannot be represented exactly as binary fractions. The decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine. That being said, you'll see that floating-point arithmetic is NOT 100% accurate.

To solve this problem, we need an alternative to server- side script numbers. That's why we have a BigDecimal Number Object and library, to address this issue. It provides arbitrary-precision decimal arithmetic.

Using server- side script double precision aritmetic

Copy
var mRow = Ax.db.executeQuery(
<select>
<columns>
CAST(0.02 AS DECIMAL) value1,
CAST(0.03 AS DECIMAL) value2
</columns>
<from table='systables' />
<where>
tabid = 1
</where>
</select>
).toOne();

console.log("Double precision");
console.log(0.02 - 0.03);
console.log(mRow.value1 - mRow.value2);

Using server- side script double precision aritmetic

Copy
var mRow = Ax.db.executeQuery(
<select>
<columns>
CAST(0.02 AS DECIMAL) value1,
CAST(0.03 AS DECIMAL) value2
</columns>
<from table='systables' />
<where>
tabid = 1
</where>
</select>
).toOne();

console.log("BigDecimal precision");
console.log(new Ax.sql.Decimal("0.02").subtract(new Ax.sql.Decimal("0.03")));
console.log(mRow.value1.subtract(mRow.value2));

Notice that Decimal types returned from database must be operated using its own function (add, subtract, div, mul, etc). If not, they are converted to double before operating.

BigDecimal is an alias of Ax.sql.Decimal and both are wrappers of java.math.BigDecimal.

The Ax.math.bc package contains a group of functions to perform BigDecimal operations.

## 3.1 Creating a BigDecimal

Return Method Description
BigDecimal of(String number) Translates the string representation of a BigDecimal into a BigDecimal. The string representation consists of an optional sign, '+' ( '\u002B') or '-' ('\u002D'), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent.
BigDecimal of(Number number) Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.
Copy
var mBcValue1 = Ax.math.bc.of("1.4");
var mBcValue2 = Ax.math.bc.of(1.4);

console.log(BigDecimal value1 : [${mBcValue1}]); console.log(BigDecimal value2 : [${mBcValue2}]);

Syntax add(Number n1, Number n2, ...)
Return A BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).
Copy
var mBcValue1 = 0.825;
var mBcValue2 = 0.175;

## 3.4 Multiplication BigDecimal

BigDecimal subtraction function: Ax.math.bc.mul

mul() FUNCTION
Syntax mul(Number n1, Number n2, ...)
Return A BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
Copy
var mBcValue1 = 0.125;
var mBcValue2 = 0.111;

var mBcTotal = Ax.math.bc.mul(mBcValue1, mBcValue2);

## 3.6 Absolute BigDecimal

BigDecimal absolute function: Ax.math.bc.abs

abs() FUNCTION
Syntax abs(Number n1)
Return A BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().
Copy
var mBcValue = -6;

var mBcAbsolute = Ax.math.bc.abs(mBcValue);

## 3.8 Comparing numbers

Be carefull when comparing BigDecimal to a server side script number. You can use:

• equals - takes scale into account

Copy
var mBoolResult1 = Ax.math.bc.equals(new Ax.math.BigDecimal(0), 0.00);
var mBoolResult2 = Ax.math.bc.equals(new Ax.math.BigDecimal(0.0), 0.00);
var mBoolResult3 = Ax.math.bc.equals(new Ax.math.BigDecimal(0.00), 0.00);
var mBoolResult4 = Ax.math.bc.equals(new Ax.math.BigDecimal(0), 0);
var mBoolResult5 = Ax.math.bc.equals(new Ax.math.BigDecimal(0.00), new Ax.math.BigDecimal(0.00));

console.log(Ax.math.bc.equals(new Ax.math.BigDecimal(0), 0.00) : [${mBoolResult1}]); console.log(Ax.math.bc.equals(new Ax.math.BigDecimal(0.0), 0.00) : [${mBoolResult2}]);
console.log(Ax.math.bc.equals(new Ax.math.BigDecimal(0.00), 0.00) : [${mBoolResult3}]); console.log(Ax.math.bc.equals(new Ax.math.BigDecimal(0), 0) : [${mBoolResult4}]);
console.log(Ax.math.bc.equals(new Ax.math.BigDecimal(0.00), new Ax.math.BigDecimal(0.00)) : [${mBoolResult5}]); • compareTo - does not take scale but returns 0, 1 or -1 accoding if number is equal, greater or lesser Copy var mIntResult1 = Ax.math.bc.compareTo(new Ax.math.BigDecimal(0), 0.00); var mIntResult2 = Ax.math.bc.compareTo(new Ax.math.BigDecimal(0.0), 0.00); var mIntResult3 = Ax.math.bc.compareTo(new Ax.math.BigDecimal(0.00), 0.00); var mIntResult4 = Ax.math.bc.compareTo(new Ax.math.BigDecimal(1), 0.00); var mIntResult5 = Ax.math.bc.compareTo(new Ax.math.BigDecimal(0), 1.00); console.log(Ax.math.bc.compareTo(new Ax.math.BigDecimal(0), 0.00) : [${mIntResult1}]);
console.log(Ax.math.bc.compareTo(new Ax.math.BigDecimal(0.0), 0.00) : [${mIntResult2}]); console.log(Ax.math.bc.compareTo(new Ax.math.BigDecimal(0.00), 0.00) : [${mIntResult3}]);
console.log(Ax.math.bc.compareTo(new Ax.math.BigDecimal(1), 0.00) : [${mIntResult4}]); console.log(Ax.math.bc.compareTo(new Ax.math.BigDecimal(0), 1.00) : [${mIntResult5}]);

• isZero - compares if number is zero

Copy
var mBoolResult1 = Ax.math.bc.isZero(new Ax.math.BigDecimal(0));
var mBoolResult2 = Ax.math.bc.isZero(new Ax.math.BigDecimal(0.00));

console.log(Ax.math.bc.isZero(new Ax.math.BigDecimal(0)) : [${mBoolResult1}]); console.log(Ax.math.bc.isZero(new Ax.math.BigDecimal(0.00)) : [${mBoolResult2}]);

## 3.9 Scaling BigDecimal

To set the number of digits after the decimal, use the setScale(scale) method. However, it is good practice to also specify the rounding mode along with the scale by using setScale(scale, roundingMode). The rounding mode specifies how to round the number.

Copy
function __setScale(pBcValue, pStrRounding)
{
var mBcResult = pBcValue.setScale(2, pStrRounding);
console.log(Ax.text.String.format("%6s %6s %s", pBcValue, mBcResult, pStrRounding));
}

for (var mRowMode of Ax.math.bc.RoundingMode) {
if (mRowMode[1] == Ax.math.bc.RoundingMode.UNNECESSARY)
continue;

__setScale(Ax.math.bc.of("0.333"), mRowMode[1]);
__setScale(Ax.math.bc.of("-0.333"), mRowMode[1]);
}

The following table shows results of scaling a value to tow deciml places on each mode

Mode Description 0.333 -0.333
CEILING Ceiling function 0.34 -0.33
UP .. 0.34 -0.34
DOWN Round towards zero 0.33 -0.33
FLOOR Floor function 0.33 -0.34
HALF_UP Round up if decimal >= .5 0.33 -0.33
HALF_DOWN Round up if decimal > .5 0.33 -0.33
HALF_EVEN Round half even will round as normal. However, when the rounding digit is 5, it will round down if the digit to the left of the 5 is even and up otherwise 0.33 -0.33
UNNECESSARY When you need to use one of the methods that requires input of a rounding mode but you know the result won't need to be rounded. RoundingNeccesary RoundingNeccesary