http://www.zorba-xquery.com/modules/excel/math
import module namespace excel = "http://www.zorba-xquery.com/modules/excel/math";
This is a library module offering a part of the set of functions defined by Microsoft Excel 2003.
Daniel Turcanu
xquery version "3.0" encoding "utf-8";
- the XQuery module can be found here.
For more details please also see:
| excel | http://www.zorba-xquery.com/modules/excel/math |
| excel-err | http://www.zorba-xquery.com/modules/excel/errors |
| ver | http://www.zorba-xquery.com/options/versioning |
|
abs
(
$arg as xs:anyAtomicType
) as xs:anyAtomicType Compute the abs of a numeric value. |
|
|
cast-as-numeric
(
$number as xs:anyAtomicType
) as xs:anyAtomicType Cast the xs:anyAtomicType to a numeric type. |
|
|
ceiling
(
$number as xs:anyAtomicType,
$significance as xs:anyAtomicType
) as xs:anyAtomicType Returns number rounded up, away from zero, to the nearest multiple of significance. |
|
|
degrees
(
$radian as xs:double
) as xs:integer Converts radians into degrees. |
|
|
even
(
$number as xs:anyAtomicType
) as xs:anyAtomicType Returns number rounded up to the nearest even integer. |
|
|
fact
(
$number as xs:anyAtomicType
) as xs:integer Returns the factorial of a number. |
|
|
factdouble
(
$number as xs:integer
) as xs:integer Returns the double factorial of a number. |
|
|
floor
(
$number as xs:anyAtomicType,
$significance as xs:anyAtomicType
) as xs:anyAtomicType Rounds number down, toward zero, to the nearest multiple of significance. |
|
|
gcd
(
$numbers as xs:integer+
) as xs:integer Returns the greatest common divisor GCD of a sequence of integers. |
|
|
int
(
$number as xs:anyAtomicType
) as xs:integer Rounds a number down to the nearest integer. |
|
|
is-a-number
(
$value as xs:anyAtomicType
) as xs:boolean Checks if the xs:anyAtomicType argument is actually a numeric type or can be converted to numeric. |
|
|
lcm
(
$numbers as xs:integer+
) as xs:integer Returns the least common multiple of integers. |
|
|
mod
(
$number as xs:anyAtomicType,
$divisor as xs:anyAtomicType
) as xs:anyAtomicType Returns the remainder after number is divided by divisor. |
|
|
mround
(
$number as xs:anyAtomicType,
$multiple as xs:anyAtomicType
) as xs:anyAtomicType Returns a number rounded to the desired multiple. |
|
|
odd
(
$number as xs:anyAtomicType
) as xs:integer Returns number rounded up to the nearest odd integer, away from zero. |
|
|
pi
(
) as xs:decimal Return the value of PI as decimal with 15 digits. |
|
|
power
(
$number as xs:anyAtomicType,
$power as xs:integer
) as xs:anyAtomicType Returns the result of a number raised to a power. |
|
|
product
(
$numbers as xs:anyAtomicType*
) as xs:anyAtomicType Multiplies all the numbers given as arguments and returns the product. |
|
|
quotient
(
$numerator as xs:anyAtomicType,
$denominator as xs:anyAtomicType
) as xs:integer Returns the integer portion of a division. |
|
|
radians
(
$degree as xs:integer
) as xs:decimal Converts degrees to radians. |
|
|
roman
(
$number as xs:integer
) as xs:string Converts an arabic numeral to roman, as text. |
|
|
round
(
$number as xs:anyAtomicType,
$precision as xs:integer
) as xs:anyAtomicType Rounds a number to a specified number of digits. |
|
|
rounddown
(
$number as xs:anyAtomicType,
$precision as xs:integer
) as xs:anyAtomicType Rounds a number down, toward zero. |
|
|
roundup
(
$number as xs:anyAtomicType,
$precision as xs:integer
) as xs:anyAtomicType Rounds a number up, away from 0 (zero). |
|
|
sign
(
$number as xs:anyAtomicType
) as xs:integer Determines the sign of a number. |
|
|
sort-numbers
(
$numbers as xs:anyAtomicType*
) as xs:anyAtomicType* Helper function. |
|
|
sum
(
$numbers as xs:anyAtomicType*
) as xs:anyAtomicType Adds all the numbers in the sequence. |
|
|
trunc
(
$number as xs:anyAtomicType
) as xs:integer Truncates a number to an integer by removing the fractional part of the number. |
|
|
trunc
(
$number as xs:anyAtomicType,
$precision as xs:integer
) as xs:anyAtomicType Truncates a number down to precision. |
declare function excel:abs (
$arg as xs:anyAtomicType
) as xs:anyAtomicType
Compute the abs of a numeric value. The value can also be a string and it will be casted to the appropriate numeric first.
- $arg The parameter can be a number, string, boolean value.
- The abs value as a numeric type.
- excel-err:Value if arg cannot be casted to numeric type.
declare function excel:cast-as-numeric (
$number as xs:anyAtomicType
) as xs:anyAtomicType
Cast the xs:anyAtomicType to a numeric type. If the value is already of a numeric type then nothing is changed. Otherwise the value is casted to the numeric type that is most appropriate.
- $number The parameter can be a number, string, boolean value.
- The casted value.
- excel-err:Value if the value cannot be casted to numeric type.
declare function excel:ceiling ( $number as xs:anyAtomicType, $significance as xs:anyAtomicType ) as xs:anyAtomicType
Returns number rounded up, away from zero, to the nearest multiple of significance. Significance must have the same sign as number. Number and significance must be of a numeric type or castable to numeric. Significance must not be zero.
- $number The value you want to round.
- $significance The multiple to which you want to round.
- The rounded value.
- excel-err:Value if parameters cannot be casted to numeric type.
- excel-err:Num if significance is zero or it doesn't have the same sign as number.
declare function excel:degrees (
$radian as xs:double
) as xs:integer
Converts radians into degrees.
- $radian The value in radians.
- The value in degrees 0 .. 360 or 0 .. -360.
declare function excel:even (
$number as xs:anyAtomicType
) as xs:anyAtomicType
Returns number rounded up to the nearest even integer. Regardless of the sign of number, a value is rounded up when adjusted away from zero.
- $number The value to round.
- The rounded value casted as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:fact (
$number as xs:anyAtomicType
) as xs:integer
Returns the factorial of a number.
- $number The nonnegative number you want the factorial of. If number is not an integer, it is truncated.
- Returns the factorial of a number. The factorial of a number is equal to 1*2*3*...* number.
- excel-err:Num if the number is smaller than zero
declare function excel:factdouble (
$number as xs:integer
) as xs:integer
Returns the double factorial of a number. Computes the double factorial of n as n(n-2)(n-4)...
- $number The positive integer value.
- The result as integer.
- excel-err:Num if the number is negative.
declare function excel:floor ( $number as xs:anyAtomicType, $significance as xs:anyAtomicType ) as xs:anyAtomicType
Rounds number down, toward zero, to the nearest multiple of significance. Significance must have the same sign as number.
- $number The value you want to round. The value is casted to numeric.
- $significance The multiple to which you want to round.
- The rounded value as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
- excel-err:Num if significance is zero or it doesn't have the same sign as number.
declare function excel:gcd (
$numbers as xs:integer+
) as xs:integer
Returns the greatest common divisor GCD of a sequence of integers. The sequence can have one or more positive integers.
- $numbers The sequence of positive integers.
- The GCD as integer.
- excel-err:Num if any number is smaller than zero.
declare function excel:int (
$number as xs:anyAtomicType
) as xs:integer
Rounds a number down to the nearest integer. Positive numbers are rounded toward zero, negative numbers are rounded away from zero.
- $number The value to be rounded.
- The rounded integer.
- excel-err:Value if parameter cannot be casted to numeric type
declare function excel:is-a-number (
$value as xs:anyAtomicType
) as xs:boolean
Checks if the xs:anyAtomicType argument is actually a numeric type or can be converted to numeric.
- $value Parameter to be checked.
- true if the value can be casted to numeric.
declare function excel:lcm (
$numbers as xs:integer+
) as xs:integer
Returns the least common multiple of integers.
LCM for two numbers is computed by multiplying them and dividing with GCD.
The function is applied recursively replacing the first two numbers in the sequence with their LCM.
- $numbers The sequence of one or more positive integers.
- The LCM as integer.
- excel-err:Num if any number is smaller than zero.
declare function excel:mod ( $number as xs:anyAtomicType, $divisor as xs:anyAtomicType ) as xs:anyAtomicType
Returns the remainder after number is divided by divisor. The result has the same sign as divisor.
- $number The number for which you want to find the remainder.
- $divisor The number by which you want to divide number. This cannot be zero.
- The remainder from division as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
- excel-err:Div0 if divisor is zero after casting to numeric.
declare function excel:mround ( $number as xs:anyAtomicType, $multiple as xs:anyAtomicType ) as xs:anyAtomicType
Returns a number rounded to the desired multiple. MROUND rounds up, away from zero, if the remainder of dividing number by multiple is greater than or equal to half the value of multiple. MROUND is computed through floor function.
- $number The value to round, castable to numeric type.
- $multiple The multiple to which you want to round number.
- The rounded number up to the desired multiple.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:odd (
$number as xs:anyAtomicType
) as xs:integer
Returns number rounded up to the nearest odd integer, away from zero.
- $number The value to round.
- The odd integer.
- excel-err:Value if parameter cannot be casted to numeric type.
declare function excel:pi ( ) as xs:decimal
Return the value of PI as decimal with 15 digits.
- The value of PI with 15 digits.
declare function excel:power ( $number as xs:anyAtomicType, $power as xs:integer ) as xs:anyAtomicType
Returns the result of a number raised to a power. The result is computed through successive multiplications.
- $number The base number.
- $power The exponent as integer (cannot be floating point like in Excel).
- The result as numeric type.
- excel-err:Value if parameter cannot be casted to numeric type.
- excel-err:Value if power is smaller than zero.
declare function excel:product (
$numbers as xs:anyAtomicType*
) as xs:anyAtomicType
Multiplies all the numbers given as arguments and returns the product.
- $numbers The sequence of arguments convertable to numeric types. The sequence can be of any length.
- The multiplication result as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:quotient ( $numerator as xs:anyAtomicType, $denominator as xs:anyAtomicType ) as xs:integer
Returns the integer portion of a division.
- $numerator The divident.
- $denominator The divisor. It cannot be zero.
- The result value as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
- excel-err:Div0 if denominator casted as numeric type has value zero.
declare function excel:radians (
$degree as xs:integer
) as xs:decimal
Converts degrees to radians.
- $degree An angle in degrees that you want to convert.
- The value in radians.
declare function excel:roman (
$number as xs:integer
) as xs:string
Converts an arabic numeral to roman, as text.
Only the clasic format is supported (out of all formats Excel requires).
M is the largest digit, it represents 1000.
Numbers bigger than 2000 will be represented by a sequence of "M".
D = 500, C = 100, L = 50, X = 10, V = 5, I = 1.
- $number A positive integer.
- The roman string representation.
- excel-err:Num if the input integer is negative
declare function excel:round ( $number as xs:anyAtomicType, $precision as xs:integer ) as xs:anyAtomicType
Rounds a number to a specified number of digits. If precision is greater than 0 (zero), then number is rounded to the specified number of decimal places. If num_digits is 0, then number is rounded to the nearest integer. If num_digits is less than 0, then number is rounded to the left of the decimal point. The 0.5 is rounded away from zero.
- $number The number to round, castable to a numeric type.
- $precision The number of decimal places to keep.
- The rounded number as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:rounddown ( $number as xs:anyAtomicType, $precision as xs:integer ) as xs:anyAtomicType
Rounds a number down, toward zero. If num_digits is greater than 0 (zero), then number is rounded down to the specified number of decimal places. If num_digits is 0, then number is rounded down to the nearest integer. If num_digits is less than 0, then number is rounded down to the left of the decimal point.
- $number The number to round, castable to numeric type.
- $precision The number of decimal places to keep.
- the truncated number toward zero, as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:roundup ( $number as xs:anyAtomicType, $precision as xs:integer ) as xs:anyAtomicType
Rounds a number up, away from 0 (zero). If num_digits is greater than 0 (zero), then number is rounded down to the specified number of decimal places. If num_digits is 0, then number is rounded down to the nearest integer. If num_digits is less than 0, then number is rounded down to the left of the decimal point.
- $number The number to round, castable to numeric type.
- $precision The number of decimal places to keep.
- The truncated number away from zero, as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:sign (
$number as xs:anyAtomicType
) as xs:integer
Determines the sign of a number. Returns 1 if the number is positive, zero (0) if the number is 0, and -1 if the number is negative.
- $number The argument castable to numeric type.
- The sign as (-1, 0, 1).
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:sort-numbers (
$numbers as xs:anyAtomicType*
) as xs:anyAtomicType*
Helper function.
Sorts a sequence of numbers or arguments castable to numeric.
It first casts all arguments to numeric and then sorts ascending.
- $numbers The sequence of arguments castable to numeric.
- The sorted sequence as numeric types.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:sum (
$numbers as xs:anyAtomicType*
) as xs:anyAtomicType
Adds all the numbers in the sequence.
- $numbers The sequence of arguments castable to numeric types. The sequence can be of any length.
- The sum as numeric type.
- excel-err:Value if parameters cannot be casted to numeric type.
declare function excel:trunc (
$number as xs:anyAtomicType
) as xs:integer
Truncates a number to an integer by removing the fractional part of the number.
- $number The argument castable to numeric type.
- The integer value.
- excel-err:Value if parameter cannot be casted to numeric type.
declare function excel:trunc ( $number as xs:anyAtomicType, $precision as xs:integer ) as xs:anyAtomicType
Truncates a number down to precision. This behaves exactly like rounddown.
- $number The argument castable to numeric type.
- $precision The number of decimal places to keep .
- The integer value.
- excel-err:Value if parameter cannot be casted to numeric type.