1 Arithmetic operators

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

1.1 Addition

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.

Addition OPERATOR
Operador Xsql <add>
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'>
            <add>
                <number>20</number>
                <number>5</number>
            </add>
        </set>

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

console.log(`Total : [${mIntTotal}]`);
Total : [25]

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>
Total : [15]
Copy
var mIntValue1 = 5;
var mIntValue2 = 10;

var mIntTotal = mIntValue1 + mIntValue2;

console.log(`Total : [${mIntTotal}]`);
Total : [15]

1.2 Subtraction

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

Subtraction OPERATOR
Operador Xsql <sub>
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>
Total : [27]
Copy
var mIntValue1 = 40;
var mIntValue2 = 13;

var mIntTotal = mIntValue1 - mIntValue2;

console.log(`Total : [${mIntTotal}]`);
Total : [27]

1.3 Multiplication

The multiplication operator (*) produces the product of the operands.

Multiplication OPERATOR
Operador Xsql <mul>
Syntax Value1 * Value2 * ... Value(n)
Return Multiplication of numbers.

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

Copy
<xsql-script>
    <body>
        <set name='m_value1'>8</set>
        <set name='m_value2'>6</set>

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

        <println>Total : [<m_total/>]</println>
    </body>
</xsql-script>
Total : [48]
Copy
var mIntValue1 = 8;
var mIntValue2 = 6;

var mIntTotal = mIntValue1 * mIntValue2;

console.log(`Total : [${mIntTotal}]`);
Total : [48]

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
Operador Xsql <div>
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>
Total : [27]
Copy
var mIntValue1 = 81;
var mIntValue2 = 3;

var mIntTotal = mIntValue1 / mIntValue2;

console.log(`Total : [${mIntTotal}]`);
Total : [27]

1.5 Remainder

The remainder operator (%) returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.

Remainder OPERATOR
Operador Xsql <mod>
Syntax Value1 % Value2
Return The remainder of the division.
Copy
<xsql-script>
    <body>
        <set name='m_value1'>13</set>
        <set name='m_value2'>5</set>

        <set name='m_remainder'>
            <mod>
                <m_value1/>
                <m_value2/>
            </mod>
        </set>

        <println>Remainder : [<m_remainder/>]</println>
    </body>
</xsql-script>
Remainder : [3]
Copy
var mIntValue1 = 13;
var mIntValue2 = 5;

var mIntRemainder = mIntValue1 % mIntValue2;

console.log(`Remainder : [${mIntRemainder}]`);
Remainder : [3]

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'>
            <add>
                <m_value/>
                1
            </add>
        </set>

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

mIntValue++;

console.log(`Value : [${mIntValue}]`);
Value : [6]

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}]`);
Result of the operation: [12]
Final value of variable mIntValue : [6]

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}]`);
Result of the operation : [10]
Final value of variable mIntValue : [6]

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>
Value : [19]
Copy
var mIntValue = 20;

mIntValue--;

console.log(`Value : [${mIntValue}]`);
Value : [19]

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}]`);
Result of the operation: [36]
Final value of variable mIntValue : [18]

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}]`);
Result of the operation : [38]
Final value of variable mIntValue : [18]

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>
Value negative : [-17]
Copy
var mIntValue = 17;

var mIntNegative = - mIntValue;

console.log(`Value negative : [${mIntNegative}]`);
Value negative : [-17]

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>
e value : [2.718281828459045]
Copy
console.log(`E value : [${Math.E}]`);
E value : [2.718281828459045]

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>
Ln(2) = [0.6931471805599453]
Copy
console.log(`Ln(2) = [${Math.LN2}]`);
Ln(2) = [0.6931471805599453]

2.1.3 Ln10

The Math.LN10 property represents the natural logarithm of 10, which is approximately 2.30258.

ln10 PROPERTY
Syntax Math.LN10
Return Approximately 2.302585092994046
Copy
<xsql-script>
    <body>
        <set name='m_value'>10</set>

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

        <println>Ln(<m_value/>) = [<m_ln10 />]</println>
    </body>
</xsql-script>
Ln(10) = [2.302585092994046]
Copy
console.log(`Ln(10) = [${Math.LN10}]`);
Ln(10) = [2.302585092994046]

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
console.log(`Base 2 logarithm of e : [${Math.LOG2E}]`);
Base 2 logarithm of e : [1.4426950408889634]

2.1.5 Log10e

The Math.LOG10E property represents the base 10 logarithm of e, which is approximately 0.43429.

log10e PROPERTY
Syntax Math.LOG10E
Return Approximately 0.4342944819032518
Copy
console.log(`Base 10 logarithm of e : [${Math.LOG10E}]`);
Base 10 logarithm of e : [0.4342944819032518]

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>
Pi value : [3.141592653589793]
Copy
console.log(`Pi value : [${Math.PI}]`);
Pi value : [3.141592653589793]

Math.PI returns PI (3.141592653589793) with a precision of 16 decimals.

2.1.7 Square root of 1/2

The Math.SQRT1_2 property represents the square root of 1/2, approximately 0.70710.

sqrt1_2 PROPERTY
Syntax Math.SQRT1_2
Return Approximately 0.7071067811865476
Copy
<xsql-script>
    <body>
        <set name='m_value'>0.5</set>

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

        <println>Square root of <m_value /> : [<m_sqrt1_2/>]</println>                       
    </body>
</xsql-script>
Square root of 1/2 : [0.7071067811865476]
Copy
console.log(`Square root of 1/2 : [${Math.SQRT1_2}]`);
Square root of 1/2 : [0.7071067811865476]

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>
Square root of 2 : [1.4142135623730951]
Copy
console.log(`Square root of 2 : [${Math.SQRT2}]`);
Square root of 2 : [1.4142135623730951]

2.1.9 Absolute value

The Math.abs() function returns the absolute value of a number

abs() METHOD
Operator Xsql <math.abs>
Syntax Math.abs(x)
Parameters x: a number.
Return Absolute value of a number
Copy
<xsql-script>
    <body>
        <set name='m_value'>-5</set>

        <set name='m_absolute'>
            <math.abs>
                <m_value/>
            </math.abs>
        </set>

        <println>Absolute value : [<m_absolute/>]</println>
    </body>
</xsql-script>
Absolute value : [5]
Copy
var mIntValue = -5;

var mIntAbsolute = Math.abs(mIntValue);

console.log(`Absolute value : [${mIntAbsolute}]`);
Absolute value : [5]

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_adjacent'>1</set>
        <set name='m_hypotenuse'>2</set>

        <set name='m_arccosine'>
            <math.acos>
                <div><m_adjacent/><m_hypotenuse/></div>
            </math.acos>
        </set>

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

    </body>
</xsql-script>
Arccosine (in radians) : [1.5707963267948966]
Copy
var mIntAdjacent = 1;
var mIntHypotenuse = 2;

var mBcArccosine = Math.acos(Ax.math.bc.div(mIntAdjacent, mIntHypotenuse));

console.log(`Arccosine (in radians) : [${mBcArccosine}]`);
Arccosine (in radians) : [1.0471975511965979]

2.1.11 Asin

The Math.asin() function returns the arcsine (in radians) of a number.

asin() METHOD
Operator Xsql <math.asin>
Syntax Math.asin(x)
Parameters x: a number.
Return Arcsine of a number in radians.
Copy
<xsql-script>
    <body>
        <set name='m_adjacent'>1</set>
        <set name='m_hypotenuse'>1</set>

        <set name='m_arcsine'>
            <math.asin>
                <div><m_adjacent/><m_hypotenuse/></div>
            </math.asin>
        </set>

        <println>Arcsine (in radians) : [<m_arcsine />]</println>
    </body>
</xsql-script>
Arcsine (in radians) : [1.5707963267948966]
Copy
var mIntAdjacent = 1;
var mIntHypotenuse = 1;

var mBcArcsine = Math.asin(Ax.math.bc.div(mIntAdjacent, mIntHypotenuse));

console.log(`Arcsine (in radians) : [${mBcArcsine}]`);
Arcsine (in radians) : [1.5707963267948966]

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_adjacent'>3</set>

        <set name='m_arctangent'>
            <math.atan>
                <div><m_opposite/><m_adjacent/></div>
            </math.atan>
        </set>

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

var mBcArctangent = Math.atan(Ax.math.bc.div(mIntOpposite, mIntAdjacent));

console.log(`Arctangent (in radians) : [${mBcArctangent}]`);
Arctangent (in radians) : [1.0303768265243125]

2.1.13 Ceil

The Math.ceil() function always rounds a number up to the next largest integer.

ceil() METHOD
Operator Xsql <math.ceiling>
Syntax Math.ceil(x)
Parameters x: a number.
Return Rounded number.
Copy
<xsql-script>
    <body>
        <set name='m_value'>1.1436</set>

        <set name='m_value_rounded'>
            <math.ceiling>
                <m_value/>
            </math.ceiling>
        </set>

        <println>Value rounded : [<m_value_rounded/>]</println>                       
    </body>
</xsql-script>
Value rounded : [2]
Copy
var mBcValue = 1.1436;

var mIntRound = Math.ceil(mBcValue);

console.log(`Value rounded : [${mIntRound}]`);
Value rounded : [2]

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>
Cos(1) = [0.5403023058681398]
Copy
var mIntAngle = 1;

var mBcCos = Math.cos(mIntAngle);

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

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>
Total : [256.0]
Copy
var mIntBase = 2;
var mIntExponent = 8;

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

console.log(`Total : [${mIntTotal}]`);
Total : [256]

2.1.16 Floor

The Math.floor() function returns the largest integer less than or equal to a given number.

floor() METHOD
Operator Xsql <math.floor>
Syntax Math.floor(x)
Parameters x: a number.
Return Largest integer.
Copy
<xsql-script>
    <body>
        <set name='m_value'>6.5468</set>

        <set name='m_value_rounded'>
            <math.floor>
                <m_value/>
            </math.floor>
        </set>

        <println>Value rounded : [<m_value_rounded/>]</println>                       
    </body>
</xsql-script>
Value rounded : [6]
Copy
var mBcValue = 6.5468;

var mIntRound = Math.floor(mBcValue);

console.log(`Value rounded : [${mIntRound}]`);
Value rounded : [6]

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>
Ln(10) = [2.302585092994046].
Copy
var mIntValue = 10;

var mBcLogarithm = Math.log(mIntValue);

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

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>
Max value : [6]
Copy
var mIntValue1 = 3;
var mIntValue2 = 6;

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

console.log(`Max value : [${mIntMaxValue}]`);
Max value : [6]

2.1.19 Min

The static function Math.min() returns the lowest-valued number passed into it.

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

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

var mIntMinValue = Math.min(mIntValue1, mIntValue2);

console.log(`Min value : [${mIntMinValue}]`);
Min value : [3]

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>
Random value : [0.45711495574018524]
Copy
var mBcValue = Math.random();

console.log(`Random value : [${mBcValue}]`);
Random value : [0.49694601654217696]

2.1.21 Rounding

The Math.round() function returns the value of a number rounded to the nearest integer.

round() METHOD
Operator Xsql <math.round>
Syntax Math.round()
Return Number rounded.
Copy
<xsql-script>
    <body>
        <set name='m_value'>6.5468</set>

        <set name='m_value_rounded'>
            <math.round>
                <m_value/>
            </math.round>
        </set>

        <println>Value rounded : [<m_value_rounded/>]</println>                       
    </body>
</xsql-script>
Value rounded : [7]
Copy
var mBcValue = 6.5468;

var mIntRound = Math.round(mBcValue);

console.log(`Value rounded : [${mIntRound}]`);
Value rounded : [7]

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>
Sin(1) = [0.8414709848078965]
Copy
var mIntAngle = 1;

var mBcSin = Math.sin(mIntAngle);

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

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>
Square root : [8.0]
Copy
var mIntValue = 64;

var mIntSquareRoot = Math.sqrt(mIntValue);

console.log(`Square root : [${mIntSquareRoot}]`);
Square root : [8]

2.1.24 Tan

The Math.tan() function returns the tangent of a number.

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

        <set name='m_tan'>
            <math.tan>
                <m_angle/>
            </math.tan>
        </set>

        <println>Tan(<m_angle />) = [<m_tan />]</println>
    </body>
</xsql-script>
Tan(1) = [1.5574077246549023]
Copy
var mIntAngle = 1;

var mBcTan = Math.tan(mIntAngle);

console.log(`Tan(${mIntAngle}) = [${mBcTan}]`);
Tan(1) = [1.5574077246549023]

2.2 Number

Number is a primitive wrapper object used to represent and manipulate numbers, and the constructor contains constants and methods for working with them.

2.2.1 Epsilon

The Number.EPSILON property returns the difference between 1 and the smallest floating point number greater than 1.

epsilon PROPERTY
Syntax Number.EPSILON
Return Approximately 2.2204460492503130808472633361816E-16.
Copy
var mValue1 = Math.abs(0.5 - 0.6 + 0.15);

var mResult = mValue1 < Number.EPSILON;

console.log(`Result : [${mResult}]`);
Result : [false]

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
console.log(`Maximum value : [${Number.MAX_VALUE}]`);
Maximum value : [1.7976931348623157e+308]

2.2.3 MinValue

The Number.MIN_VALUE property returns the smallest positive number possible in server- side script.

min_value PROPERTY
Syntax Number.MIN_VALUE
Return Approximately 5e-324.

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

Copy
console.log(`Minimum value : [${Number.MIN_VALUE}]`);
Minimum value : [5e-324]

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>
m_value_tofixed = 16.18
Copy
var mBcValue1 = 16.1784;

console.log(`Result : [${mBcValue1.toFixed(2)}]`);
Result : [16.18]

2.2.5 toPrecision

The toPrecision() method formats a number to a specified length.

toPrecision() METHOD
Syntax Value.toPrecision(x)
Parameters x: the number of digits to format.
Return A number formated.
Copy
var mBcValue1 = 16.1784;

console.log(`Result : [${mBcValue1.toPrecision(3)}]`);
Result : [16.2]

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>
m_value_integer [5] : java.lang.Integer
m_value_to_string [5] : java.lang.String
Copy
var mIntValue = 12;

var mStrValue = mIntValue.toString();

console.log(`Value : [${mStrValue}]`);
String value : [12]

2.2.7 valueOf

The valueOf() method returns the primitive value of a number.

valueOf() METHOD
Syntax number.valueOf()
Return A Number, representing the primitive value of a number.
Copy
<xsql-script name='valueof_sample1'>
    <body>
        <set name="m_value_1" type="integer">5</set>
        
        <set name="m_value_2"><m_value_1 /></set>      
        
        <println>m_value_2 : [<m_value_2 />]</println>
    </body>
</xsql-script>
m_value_2 : [5]
Copy
var mIntValue1 = 5;

var mIntValue2 = mIntValue1.valueOf();

console.log(`mIntValue2: [${mIntValue2}]`);
mIntValue2 : [5]

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);
Double precision
-0.009999999999999998
-0.009999999999999998

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));
BigDecimal precision
-0.01
-0.01

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}]`);
BigDecimal value1 : [1.4]
BigDecimal value2 : [1.4]

3.2 Addition BigDecimal

BigDecimal addiction function: Ax.math.bc.add

add() FUNCTION
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;

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

console.log(`Total : [${mBcTotal}]`);
Total : [1.000]

3.3 Subtraction BigDecimal

BigDecimal subtraction function: Ax.math.bc.sub

sub() FUNCTION
Syntax sub(Number n1, Number n2, ...)
Return A BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()).
Copy
var mBcValue1 = 1;
var mBcValue2 = 0.175;

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

console.log(`Total : [${mBcTotal}]`);
Total : [0.825]

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);

console.log(`Total : [${mBcTotal}]`);
Total : [0.013875]

3.5 Division BigDecimal

BigDecimal division function: Ax.math.bc.div

div() FUNCTION
Syntax div(Number n1, Number n2)
Return A BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.
Copy
var mBcValue1 = 0.333;
var mBcValue2 = 0.777;

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

console.log(`Total : [${mBcTotal}]`);
Total : [0.4285714285714286]

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);

console.log(`Absolute value : [${mBcAbsolute}]`);
Absolute value : [6]

3.7 Exponentiation BigDecimal

BigDecimal exponentiation function: Ax.math.bc.pow

pow() FUNCTION
Syntax pow(Number n1, int n2)
Return A BigDecimal whose value is (thisn), the power is computed exactly, to unlimited precision.
Copy
var mIntBase = 2.45321;
var mIntExponent = 8;

var mIntTotal = Ax.math.bc.pow(mIntBase, mIntExponent);

console.log(`Total : [${mIntTotal}]`);
Total : [1311.8307616763383590678847194799515448637761]

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}]`);
Ax.math.bc.equals(new Ax.math.BigDecimal(0), 0.00) : [false]
Ax.math.bc.equals(new Ax.math.BigDecimal(0.0), 0.00) : [false]
Ax.math.bc.equals(new Ax.math.BigDecimal(0.00), 0.00) : [false]
Ax.math.bc.equals(new Ax.math.BigDecimal(0), 0) : [true]
Ax.math.bc.equals(new Ax.math.BigDecimal(0.00), new Ax.math.BigDecimal(0.00)) : [true]

  • 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}]`);
Ax.math.bc.compareTo(new Ax.math.BigDecimal(0), 0.00) : [0]
Ax.math.bc.compareTo(new Ax.math.BigDecimal(0.0), 0.00) : [0]
Ax.math.bc.compareTo(new Ax.math.BigDecimal(0.00), 0.00) : [0]
Ax.math.bc.compareTo(new Ax.math.BigDecimal(1), 0.00) : [1]
Ax.math.bc.compareTo(new Ax.math.BigDecimal(0), 1.00) : [-1]

  • 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}]`);
Ax.math.bc.isZero(new Ax.math.BigDecimal(0)) : [true]
Ax.math.bc.isZero(new Ax.math.BigDecimal(0.00)) : [true]

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]);
}
0.333   0.33 DOWN
-0.333  -0.33 DOWN
 0.333   0.33 FLOOR
-0.333  -0.34 FLOOR
 0.333   0.34 CEILING
-0.333  -0.33 CEILING
 0.333   0.33 HALF_EVEN
-0.333  -0.33 HALF_EVEN
 0.333   0.34 UP
-0.333  -0.34 UP
 0.333   0.33 HALF_UP
-0.333  -0.33 HALF_UP
 0.333   0.33 HALF_DOWN
-0.333  -0.33 HALF_DOWN

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