Because the Math (or Mathematical) functions category has too many functions, I decide to break this blog post into different parts, so welcome to the second part!
Overview
Math (or Mathematical) functions are used to perform a variety of mathematical and scientific operations, such as addition and multiplication. If you come from the BizTalk Server background or are migrating BizTalk Server projects, they are the equivalent of Mathematical and Scientific Functoids inside BizTalk Mapper Editor.
Available Functions
The Math functions are:
- Absolute: Returns the absolute value of the specified number.
- Add: Returns the sum from adding two or more numbers.
- Arctangent: Returns the arc tangent of a number.
- Ceiling: Returns the smallest integral value greater than or equal to the specified number.
- Cosine: Returns the cosine for the specified angle.
- Divide: Returns the result from dividing two numbers.
- Exponential: Raises the “e” constant to the specified power and returns the result.
- Exponential (base 10): Returns the number 10 raised to the specified power.
- Floor: Returns the largest integral value less than or equal to the specified number.
- Integer divide: Divides two numbers and returns the integer part from the result.
- Log: Returns the logarithm for the specified number in the specified base.
- Log (base 10): Returns the base 10 logarithm for the specified number.
- Modulo: Returns the remainder from dividing the specified numbers.
- Multiply: Returns the product from multiplying two or more specified numbers.
- Power: Returns the specified number raised to the specified power.
- Round: Rounds a value to the nearest integer or the specified number of fractional digits and returns the result.
- Sine: Returns the sine for the specified angle.
- Square root: Returns the square root for the specified number.
- Subtract: Subtracts the second number from the first number and returns the result.
- Tangent: Returns the tangent for the specified angle.
Log
This function states that it will return the logarithm for the specified number in the specified base.
Behind the scenes, this function is translated to the following XPath function: math:log($arg)
math:log
($arg as xs:double?
)as xs:double?
Rules:
- The result is the natural logarithm of
$arg
Sample:
- The expression
math:log(0)
returnsxs:double('-INF')
. - The expression
math:log(-1)
returnsxs:double('NaN')
. - The expression
math:log(2)
returns0.6931471805599453
Log (base 10)
This function states that it will return the base 10 logarithm for the specified number.
Behind the scenes, this function is translated to the following XPath function: math:log10($arg)
math:log10
($arg as xs:double?
)as xs:double?
Rules:
- The result is the base-10 logarithm of
$arg
Sample:
- The expression
math:log10(0)
returnsxs:double('-INF')
- The expression
math:log10(2)
returns0.3010299956639812
- The expression
math:log10(-1)
returnsxs:double('NaN')
Modulo
This function states that it will return the remainder from dividing the specified numbers.
Behind the scenes, this function is translated to the following XPath expression: ($arg1) mod ($arg2)
fn:error
($code as xs:QName?
,$description as xs:string
)as none
Rules:
- Returns the remainder resulting from dividing
$arg1
, the dividend, by$arg2
, the divisor. - The operation
a mod b
for operands that arexs:integer
orxs:decimal
, or types derived from them, produces a result such that(a idiv b)*b+(a mod b)
is equal toa
and the magnitude of the result is always less than the magnitude ofb
. This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0). It follows from this rule that the sign of the result is the sign of the dividend.
Sample:
- The expression
(10) mod (3)
returns1
. - The expression
(6) mod (-2)
returns0
. - The expression
(4.5) mod (1.2)
returns0.9
.
Multiply
This function states that it will return the product from multiplying two or more specified numbers.
Behind the scenes, this function is translated to the following XPath expression: ($arg1) * ($arg2) (allows more inputs)
- ($arg1) * ($arg2)
as xs:numeric?
Rules:
- Returns the arithmetic product of its operands: (
$arg1 * $arg2
). - For the four types
xs:float
,xs:double
,xs:decimal
andxs:integer
, it is guaranteed that if the type of$arg
is an instance of type T then the result will also be an instance of T. The result may also be an instance of a type derived from one of these four by restriction. For example, if$arg
is an instance ofxs:decimal
then the result may be an instance ofxs:integer
. - This function allows two or more inputs.
Sample:
- The expression (
5) + (2)
returns10
. - The expression (
5.1) + (2)
returns10.2
.
Power
This function states that it will return the specified number raised to the specified power.
Behind the scenes, this function is translated to the following XPath function: math:pow($arg1, $arg2)
math:pow
($arg1 as xs:double?
,$arg2 as xs:numeric
)as xs:double?
Rules:
- If
$arg2
is an instance ofxs:integer
, the result is$arg1
raised to the power of$
. Otherwisearg2
$
is converted to anarg2
xs:double
by numeric promotion, and the result is the value of$
raised to the power ofarg
1$
.arg2
Sample:
- The expression
math:pow(2, 3)
returns8
. - The expression
math:pow(-2, 3)
returns-8
- The expression
math:pow(2, 0)
returns1
- The expression
math:pow(2.5, 2)
returns 6.25
Round
This function states that it will round a value to the nearest integer or the specified number of fractional digits and returns the result.
Behind the scenes, this function is translated to the following XPath function: round($arg1, $arg2)
fn:round
($arg as xs:numeric?
,$precision as xs:integer
)as xs:numeric?
Rules:
- The function returns the nearest (that is, numerically closest) value to
$arg
that is a multiple of ten to the power of minus$precision
. If two such values are equally near (for example, if the fractional part in$arg
is exactly .5), the function returns the one that is closest to positive infinity. - For the four types
xs:float
,xs:double
,xs:decimal
andxs:integer
, it is guaranteed that if the type of$arg
is an instance of type T then the result will also be an instance of T. The result may also be an instance of a type derived from one of these four by restriction. For example, if$arg
is an instance ofxs:decimal
and$precision
is less than one, then the result may be an instance ofxs:integer
.
Sample:
- The expression
fn:round(1.125, 2)
returns1.13
- The expression
fn:round(8452, -2)
returns8500
Sine
This function states that it will return the sine for the specified angle.
Behind the scenes, this function is translated to the following XPath function: math:sin($arg)
math:sin
($
argas xs:double?
)as xs:double?
Rules:
- If
$
arg is positive or negative zero, the result is$
arg. - Returns the sine of the argument. The argument is an angle in radians.
Sample:
- The expression
math:sin(0)
returns0
. - The expression
math:sin(45)
returns0.8509035245341184
.
Square root
This function states that it will return the square root for the specified number.
Behind the scenes, this function is translated to the following XPath function: math:sqrt($arg)
math:sqrt
($arg as xs:double?
)as xs:double?
Rules:
- If
$arg
is positive or negative zero, positive infinity, orNaN
, then the result is$arg
. (Negative zero is the only case where the result can have negative sign) - The result is the mathematical non-negative square root of
$arg
Sample:
- The expression
math:sqrt(0)
returns0
. - The expression
math:sqrt(-2)
returnsNaN.
- The expression
math:sqrt(4)
returns2
.
Subtract
This function states that it will subtract the second number from the first number and returns the result.
Behind the scenes, this function is translated to the following XPath function: ($arg1) - ($arg2
)
(
$arg1 as xs:numeric
-$arg2 as xs:numeric
)as xs:numeric
Rules:
- Returns the arithmetic difference of its operands: (
$arg1 - $arg2
). $arg1 and $arg2
are numeric values (xs:float
,xs:double
,xs:decimal
andxs:integer
)
Sample:
- The expression (
3) - (1)
returns2
. - The expression (2
) - (1.12)
returns0.88
.
Tangent
This function states that it will return the tangent for the specified angle.
Behind the scenes, this function is translated to the following XPath function: math:tan($arg)
math:tan
($
argas xs:double?
)as xs:double?
Rules:
- If
$
arg is positive or negative infinity, orNaN
, then the result isNaN
. - Returns the tangent of the argument. The argument is an angle in radians.
Sample:
- The expression
math:tan(0)
returns0
- The expression math:tan(12) returns
-0.6358599286615808
Hope you find this helpful! So, if you liked the content or found it useful and want to help me write more, you can buy (or help buy) my son a Star Wars Lego!