Select Page

This post was originally published here

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.
• 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)` returns `xs:double('-INF')`.
• The expression `math:log(-1)` returns `xs:double('NaN')`.
• The expression `math:log(2)` returns `0.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)` returns `xs:double('-INF')`
• The expression `math:log10(2)` returns `0.3010299956639812`
• The expression `math:log10(-1)` returns `xs: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 are `xs:integer` or `xs:decimal`, or types derived from them, produces a result such that `(a idiv b)*b+(a mod b)` is equal to `a` and the magnitude of the result is always less than the magnitude of `b`. 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)` returns `1`.
• The expression `(6) mod (-2)` returns `0`.
• The expression `(4.5) mod (1.2)` returns `0.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` and `xs: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 of `xs:decimal` then the result may be an instance of `xs:integer`.
• This function allows two or more inputs.

Sample:

• The expression (`5) + (2)` returns `10`.
• The expression (`5.1) + (2)` returns `10.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 of `xs:integer`, the result is `\$arg1` raised to the power of `\$arg2`. Otherwise `\$arg2` is converted to an `xs:double` by numeric promotion, and the result is the value of `\$arg1` raised to the power of `\$arg2`.

Sample:

• The expression `math:pow(2, 3)` returns `8`.
• The expression `math:pow(-2, 3)` returns `-8`
• The expression `math:pow(2, 0)` returns `1`
• 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` and `xs: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 of `xs:decimal` and `\$precision` is less than one, then the result may be an instance of `xs:integer`.

Sample:

• The expression `fn:round(1.125, 2)` returns `1.13`
• The expression `fn:round(8452, -2)` returns `8500`

## 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`(`\$`arg` as 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)` returns `0`.
• The expression `math:sin(45)` returns `0.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, or `NaN`, 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)` returns `0`.
• The expression `math:sqrt(-2)` returns `NaN.`
• The expression `math:sqrt(4)` returns `2`.

## 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` and `xs:integer`)

Sample:

• The expression (`3) - (1)` returns `2`.
• The expression (2`) - (1.12)` returns `0.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`(`\$`arg` as xs:double?`)` as xs:double?`

Rules:

• If `\$`arg is positive or negative infinity, or `NaN`, then the result is `NaN`.
• Returns the tangent of the argument. The argument is an angle in radians.

Sample:

• The expression `math:tan(0)` returns `0`
• 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!

##### Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc.

He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.
View all posts by Sandro Pereira