http://www.zorba-xquery.com/modules/excel/math ZC

Module Description
Before using any of the functions below please remember to import the module namespace:
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.

Author:

Daniel Turcanu

XQuery version and encoding for this module:

xquery version "3.0" encoding "utf-8";

Module Resources
  • the XQuery module can be found here.
Related Documentation

For more details please also see:

Namespaces
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
Function Summary
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.
Functions
abs back to 'Function Summary'
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.

Parameters:
  • $arg The parameter can be a number, string, boolean value.
Returns:
  • The abs value as a numeric type.
Errors:
  • excel-err:Value if arg cannot be casted to numeric type.
See:
Examples:

cast-as-numeric back to 'Function Summary'
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.

Parameters:
  • $number The parameter can be a number, string, boolean value.
Returns:
  • The casted value.
Errors:
  • excel-err:Value if the value cannot be casted to numeric type.

ceiling back to 'Function Summary'
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.

Parameters:
  • $number The value you want to round.
  • $significance The multiple to which you want to round.
Returns:
  • The rounded value.
Errors:
  • 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.
See:
Examples:

degrees back to 'Function Summary'
declare function excel:degrees (
            $radian as xs:double 
 ) as xs:integer

Converts radians into degrees.

Parameters:
  • $radian The value in radians.
Returns:
  • The value in degrees 0 .. 360 or 0 .. -360.
See:

even back to 'Function Summary'
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.

Parameters:
  • $number The value to round.
Returns:
  • The rounded value casted as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

fact back to 'Function Summary'
declare function excel:fact (
            $number as xs:anyAtomicType 
 ) as xs:integer

Returns the factorial of a number.

Parameters:
  • $number The nonnegative number you want the factorial of. If number is not an integer, it is truncated.
Returns:
  • Returns the factorial of a number. The factorial of a number is equal to 1*2*3*...* number.
Errors:
  • excel-err:Num if the number is smaller than zero
See:
Examples:

factdouble back to 'Function Summary'
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)...

Parameters:
  • $number The positive integer value.
Returns:
  • The result as integer.
Errors:
  • excel-err:Num if the number is negative.
See:
Examples:

floor back to 'Function Summary'
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.

Parameters:
  • $number The value you want to round. The value is casted to numeric.
  • $significance The multiple to which you want to round.
Returns:
  • The rounded value as numeric type.
Errors:
  • 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.
See:
Examples:

gcd back to 'Function Summary'
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.

Parameters:
  • $numbers The sequence of positive integers.
Returns:
  • The GCD as integer.
Errors:
  • excel-err:Num if any number is smaller than zero.
See:
Examples:

int back to 'Function Summary'
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.

Parameters:
  • $number The value to be rounded.
Returns:
  • The rounded integer.
Errors:
  • excel-err:Value if parameter cannot be casted to numeric type
See:
Examples:

is-a-number back to 'Function Summary'
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.

Parameters:
  • $value Parameter to be checked.
Returns:
  • true if the value can be casted to numeric.

lcm back to 'Function Summary'
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.

Parameters:
  • $numbers The sequence of one or more positive integers.
Returns:
  • The LCM as integer.
Errors:
  • excel-err:Num if any number is smaller than zero.
See:
Examples:

mod back to 'Function Summary'
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.

Parameters:
  • $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.
Returns:
  • The remainder from division as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
  • excel-err:Div0 if divisor is zero after casting to numeric.
See:
Examples:

mround back to 'Function Summary'
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.

Parameters:
  • $number The value to round, castable to numeric type.
  • $multiple The multiple to which you want to round number.
Returns:
  • The rounded number up to the desired multiple.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

odd back to 'Function Summary'
declare function excel:odd (
            $number as xs:anyAtomicType 
 ) as xs:integer

Returns number rounded up to the nearest odd integer, away from zero.

Parameters:
  • $number The value to round.
Returns:
  • The odd integer.
Errors:
  • excel-err:Value if parameter cannot be casted to numeric type.
See:
Examples:

pi back to 'Function Summary'
declare function excel:pi (
 ) as xs:decimal

Return the value of PI as decimal with 15 digits.

Returns:
  • The value of PI with 15 digits.
See:

power back to 'Function Summary'
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.

Parameters:
  • $number The base number.
  • $power The exponent as integer (cannot be floating point like in Excel).
Returns:
  • The result as numeric type.
Errors:
  • excel-err:Value if parameter cannot be casted to numeric type.
  • excel-err:Value if power is smaller than zero.
See:
Examples:

product back to 'Function Summary'
declare function excel:product (
            $numbers as xs:anyAtomicType* 
 ) as xs:anyAtomicType

Multiplies all the numbers given as arguments and returns the product.

Parameters:
  • $numbers The sequence of arguments convertable to numeric types. The sequence can be of any length.
Returns:
  • The multiplication result as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

quotient back to 'Function Summary'
declare function excel:quotient (
            $numerator as xs:anyAtomicType, 
            $denominator as xs:anyAtomicType 
 ) as xs:integer

Returns the integer portion of a division.

Parameters:
  • $numerator The divident.
  • $denominator The divisor. It cannot be zero.
Returns:
  • The result value as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
  • excel-err:Div0 if denominator casted as numeric type has value zero.
See:
Examples:

radians back to 'Function Summary'
declare function excel:radians (
            $degree as xs:integer 
 ) as xs:decimal

Converts degrees to radians.

Parameters:
  • $degree An angle in degrees that you want to convert.
Returns:
  • The value in radians.
See:
Examples:

roman back to 'Function Summary'
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.

Parameters:
  • $number A positive integer.
Returns:
  • The roman string representation.
Errors:
  • excel-err:Num if the input integer is negative
See:
Examples:

round back to 'Function Summary'
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.

Parameters:
  • $number The number to round, castable to a numeric type.
  • $precision The number of decimal places to keep.
Returns:
  • The rounded number as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

rounddown back to 'Function Summary'
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.

Parameters:
  • $number The number to round, castable to numeric type.
  • $precision The number of decimal places to keep.
Returns:
  • the truncated number toward zero, as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

roundup back to 'Function Summary'
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.

Parameters:
  • $number The number to round, castable to numeric type.
  • $precision The number of decimal places to keep.
Returns:
  • The truncated number away from zero, as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

sign back to 'Function Summary'
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.

Parameters:
  • $number The argument castable to numeric type.
Returns:
  • The sign as (-1, 0, 1).
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

sort-numbers back to 'Function Summary'
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.

Parameters:
  • $numbers The sequence of arguments castable to numeric.
Returns:
  • The sorted sequence as numeric types.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.

sum back to 'Function Summary'
declare function excel:sum (
            $numbers as xs:anyAtomicType* 
 ) as xs:anyAtomicType

Adds all the numbers in the sequence.

Parameters:
  • $numbers The sequence of arguments castable to numeric types. The sequence can be of any length.
Returns:
  • The sum as numeric type.
Errors:
  • excel-err:Value if parameters cannot be casted to numeric type.
See:
Examples:

trunc back to 'Function Summary'
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.

Parameters:
  • $number The argument castable to numeric type.
Returns:
  • The integer value.
Errors:
  • excel-err:Value if parameter cannot be casted to numeric type.
See:
Examples:

trunc back to 'Function Summary'
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.

Parameters:
  • $number The argument castable to numeric type.
  • $precision The number of decimal places to keep .
Returns:
  • The integer value.
Errors:
  • excel-err:Value if parameter cannot be casted to numeric type.
See:
Examples:

blog comments powered by Disqus