Difference between revisions of "Math Module"

From BaseX Documentation
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 10: Line 10:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:pi() as xs:double</pre>
 
|<pre>math:pi() as xs:double</pre>
 
|-
 
|-
Line 25: Line 25:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:sqrt(
 
|<pre>math:sqrt(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 37: Line 37:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:sin(
 
|<pre>math:sin(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 49: Line 49:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:cos(
 
|<pre>math:cos(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 61: Line 61:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:tan(
 
|<pre>math:tan(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 73: Line 73:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:asin(
 
|<pre>math:asin(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 85: Line 85:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:acos(
 
|<pre>math:acos(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 97: Line 97:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:atan(
 
|<pre>math:atan(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 109: Line 109:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:atan2(
 
|<pre>math:atan2(
   $arg1  as xs:double?
+
   $arg1  as xs:double?,
 
   $arg2  as xs:double
 
   $arg2  as xs:double
 
) as xs:double?</pre>
 
) as xs:double?</pre>
Line 122: Line 122:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:pow(
 
|<pre>math:pow(
   $arg1  as xs:double?
+
   $arg1  as xs:double?,
 
   $arg2  as xs:double
 
   $arg2  as xs:double
 
) as xs:double?</pre>
 
) as xs:double?</pre>
Line 139: Line 139:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:exp(
 
|<pre>math:exp(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 155: Line 155:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:log(
 
|<pre>math:log(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 171: Line 171:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:log10(
 
|<pre>math:log10(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 189: Line 189:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:e() as xs:double</pre>
 
|<pre>math:e() as xs:double</pre>
 
|-
 
|-
Line 203: Line 203:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:sinh(
 
|<pre>math:sinh(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 219: Line 219:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:cosh(
 
|<pre>math:cosh(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 235: Line 235:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:tanh(
 
|<pre>math:tanh(
 
   $arg  as xs:double?
 
   $arg  as xs:double?
Line 252: Line 252:
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
| width='120' | '''Signatures'''
+
| width='120' | '''Signature'''
 
|<pre>math:crc32(
 
|<pre>math:crc32(
 
   $string  as xs:string?
 
   $string  as xs:string?

Latest revision as of 15:59, 9 March 2023

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[edit]

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[edit]

math:pi[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

math:e[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Signature
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[edit]

Version 9.1
  • Updated: math:crc32 can be called with empty sequence.
Version 7.5
  • Moved: math:random and math:uuid have been moved to the Random Module.
Version 7.3