Difference between revisions of "Math Module"

From BaseX Documentation
Jump to navigation Jump to search
m (corrected/added link locations for changelog items)
m (Text replacement - "[http://www.w3.org/TR/xpath-functions-30" to "[https://www.w3.org/TR/xpath-functions-31")
Tags: Mobile web edit Mobile edit
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
The math [[Module Library|XQuery Module]] defines functions to perform mathematical operations, such as {{Code|pi}}, {{Code|asin}} and {{Code|acos}}. Most functions are specified in the [http://www.w3.org/TR/xpath-functions-30/ Functions and Operators Specification] of the upcoming XQuery 3.0 Recommendation, and some additional ones have been added in this module.
+
The math [[Module Library|XQuery Module]] defines functions to perform mathematical operations, such as {{Code|pi}}, {{Code|asin}} and {{Code|acos}}. Most functions are specified in the [https://www.w3.org/TR/xpath-functions-31/ Functions and Operators Specification] of the upcoming XQuery 3.0 Recommendation, and some additional ones have been added in this module.
  
 
=Conventions=
 
=Conventions=
  
All functions in this module are assigned to the {{Code|http://www.w3.org/2005/xpath-functions/math}} namespace, which is statically bound to the {{Code|math}} prefix.<br/>
+
All functions in this module are assigned to the <code><nowiki>http://www.w3.org/2005/xpath-functions/math</nowiki></code> namespace, which is statically bound to the {{Code|math}} prefix.<br/>
  
 
=W3 Functions=
 
=W3 Functions=
Line 10: Line 10:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:pi||xs:double}}
 
|{{Func|math:pi||xs:double}}
 
|-
 
|-
Line 25: Line 25:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:sqrt|$arg as xs:double?|xs:double?}}
 
|{{Func|math:sqrt|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 35: Line 35:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:sin|$arg as xs:double?|xs:double?}}
 
|{{Func|math:sin|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 45: Line 45:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:cos|$arg as xs:double?|xs:double?}}
 
|{{Func|math:cos|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 55: Line 55:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:tan|$arg as xs:double?|xs:double?}}
 
|{{Func|math:tan|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 65: Line 65:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:asin|$arg as xs:double?|xs:double?}}
 
|{{Func|math:asin|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 75: Line 75:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:acos|$arg as xs:double?|xs:double?}}
 
|{{Func|math:acos|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 85: Line 85:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:atan|$arg as xs:double?|xs:double?}}
 
|{{Func|math:atan|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 95: Line 95:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:atan2|$arg1 as xs:double?, $arg2 as xs:double|xs:double?}}
 
|{{Func|math:atan2|$arg1 as xs:double?, $arg2 as xs:double|xs:double?}}
 
|-
 
|-
Line 105: Line 105:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:pow|$arg1 as xs:double?, $arg2 as xs:double|xs:double?}}
 
|{{Func|math:pow|$arg1 as xs:double?, $arg2 as xs:double|xs:double?}}
 
|-
 
|-
Line 119: Line 119:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:exp|$arg as xs:double?|xs:double?}}
 
|{{Func|math:exp|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 133: Line 133:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:log|$arg as xs:double?|xs:double?}}
 
|{{Func|math:log|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 147: Line 147:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:log10|$arg as xs:double?|xs:double?}}
 
|{{Func|math:log10|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 163: Line 163:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:e||xs:double}}
 
|{{Func|math:e||xs:double}}
 
|-
 
|-
Line 177: Line 177:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:sinh|$arg as xs:double?|xs:double?}}
 
|{{Func|math:sinh|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 191: Line 191:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:cosh|$arg as xs:double?|xs:double?}}
 
|{{Func|math:cosh|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 205: Line 205:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
 
|{{Func|math:tanh|$arg as xs:double?|xs:double?}}
 
|{{Func|math:tanh|$arg as xs:double?|xs:double?}}
 
|-
 
|-
Line 220: Line 220:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='90' | '''Signatures'''
+
| width='120' | '''Signatures'''
|{{Func|math:crc32|$str as xs:string|xs:hexBinary}}<br />
+
|{{Func|math:crc32|$string as xs:string?|xs:hexBinary?}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Calculates the CRC32 check sum of the given string {{Code|$str}}.<br />
+
|Calculates the CRC32 check sum of the given {{Code|$string}}.<br/>If an empty sequence is supplied, the empty sequence is returned.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
Line 233: Line 233:
  
 
=Changelog=
 
=Changelog=
 +
 +
; Version 9.1
 +
* Updated: [[#math:crc32|math:crc32]] can be called with empty sequence.
  
 
; Version 7.5
 
; Version 7.5
* Moved: [[Random Module#random:integer|math:random]] and [[Random Module#random:uuid|math:uuid]] have been move to [[Random Module]].
+
* Moved: [[Random Module#random:integer|math:random]] and [[Random Module#random:uuid|math:uuid]] have been moved to [[Random Module]].
 +
 
 
; Version 7.3
 
; Version 7.3
 
* Added: [[#math:crc32|math:crc32]] and [[Random Module#random:uuid|math:uuid]] have been adopted from the obsolete Utility Module.
 
* Added: [[#math:crc32|math:crc32]] and [[Random Module#random:uuid|math:uuid]] have been adopted from the obsolete Utility Module.
 
[[Category:XQuery]]
 

Revision as of 11:32, 2 July 2020

The math XQuery Module defines functions to perform mathematical operations, such as pi, asin and acos. Most functions are specified in the Functions and Operators Specification of the upcoming XQuery 3.0 Recommendation, and some additional ones have been added in this module.

Conventions

All functions in this module are assigned to the http://www.w3.org/2005/xpath-functions/math namespace, which is statically bound to the math prefix.

W3 Functions

math:pi

Signatures math:pi() as xs:double
Summary Returns the xs:double value of the mathematical constant π whose lexical representation is 3.141592653589793.
Examples
  • 2*math:pi() returns 6.283185307179586e0.
  • 60 * (math:pi() div 180) converts an angle of 60 degrees to radians.

math:sqrt

Signatures math:sqrt($arg as xs:double?) as xs:double?
Summary Returns the square root of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the xs:double value of the mathematical square root of $arg.

math:sin

Signatures math:sin($arg as xs:double?) as xs:double?
Summary Returns the sine of the $arg, expressed in radians.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the sine of $arg, treated as an angle in radians.

math:cos

Signatures math:cos($arg as xs:double?) as xs:double?
Summary Returns the cosine of $arg, expressed in radians.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the cosine of $arg, treated as an angle in radians.

math:tan

Signatures math:tan($arg as xs:double?) as xs:double?
Summary Returns the tangent of $arg, expressed in radians.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the tangent of $arg, treated as an angle in radians.

math:asin

Signatures math:asin($arg as xs:double?) as xs:double?
Summary Returns the arc sine of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the arc sine of $arg, returned as an angle in radians in the range -π/2 to +π/2.

math:acos

Signatures math:acos($arg as xs:double?) as xs:double?
Summary Returns the arc cosine of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the arc cosine of $arg, returned as an angle in radians in the range 0 to +π.

math:atan

Signatures math:atan($arg as xs:double?) as xs:double?
Summary Returns the arc tangent of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the arc tangent of $arg, returned as an angle in radians in the range -π/2 to +π/2.

math:atan2

Signatures math:atan2($arg1 as xs:double?, $arg2 as xs:double) as xs:double?
Summary Returns the arc tangent of $arg1 divided by $arg2, the result being in the range -π/2 to +π/2 radians.
If $arg1 is the empty sequence, the empty sequence is returned.
Otherwise the result is the arc tangent of $arg1 divided by $arg2, returned as an angle in radians in the range -π to +π.

math:pow

Signatures math:pow($arg1 as xs:double?, $arg2 as xs:double) as xs:double?
Summary Returns $arg1 raised to the power of $arg2.
If $arg1 is the empty sequence, the empty sequence is returned.
Otherwise the result is the $arg1 raised to the power of $arg2.
Examples
  • math:pow(2, 3) returns 8.

math:exp

Signatures math:exp($arg as xs:double?) as xs:double?
Summary Returns e raised to the power of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the value of e raised to the power of $arg.
Examples
  • math:exp(1) returns e.

math:log

Signatures math:log($arg as xs:double?) as xs:double?
Summary Returns the natural logarithm of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the natural logarithm (base e) of $arg.
Examples
  • math:log(math:e()) returns 1.

math:log10

Signatures math:log10($arg as xs:double?) as xs:double?
Summary Returns the base 10 logarithm of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the base 10 logarithm of $arg.
Examples
  • math:log(100) returns 2.

Additional Functions

math:e

Signatures math:e() as xs:double
Summary Returns the xs:double value of the mathematical constant e whose lexical representation is 2.718281828459045.
Examples
  • 5*math:e() returns 13.591409142295225.

math:sinh

Signatures math:sinh($arg as xs:double?) as xs:double?
Summary Returns the hyperbolic sine of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the hyperbolic sine of $arg.
Examples
  • math:sinh(0) returns 0.

math:cosh

Signatures math:cosh($arg as xs:double?) as xs:double?
Summary Returns the hyperbolic cosine of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the hyperbolic cosine of $arg.
Examples
  • math:cosh(0) returns 1.

math:tanh

Signatures math:tanh($arg as xs:double?) as xs:double?
Summary Returns the hyperbolic tangent of $arg.
If $arg is the empty sequence, the empty sequence is returned.
Otherwise the result is the hyperbolic tangent of $arg.
Examples
  • math:tanh(100) returns 1.

math:crc32

Signatures math:crc32($string as xs:string?) as xs:hexBinary?
Summary Calculates the CRC32 check sum of the given $string.
If an empty sequence is supplied, the empty sequence is returned.
Examples
  • math:crc32("") returns '00000000'.
  • math:crc32("BaseX") returns '4C06FC7F'.

Changelog

Version 9.1
  • Updated: math:crc32 can be called with empty sequence.
Version 7.5
Version 7.3