Product SiteDocumentation Site

5.2.3. Arithmetics

Figure 5.16. AdditiveExpr

Figure 5.17. MultiplicativeExpr

Figure 5.18. UnaryExpr

JSONiq supports the basic four operations, integer division and modulo.
Multiplicative operations have precedence over additive operations. Parentheses can override it.
Example 5.37. Basic arithmetic operations with precedence override
1 * ( 2 + 3 ) + 7 idiv 2 - (-8) mod 2
Result (run with Zorba):

Dates, times and durations are also supported in a natural way.
Example 5.38. Using basic operations with dates.
date("2013-05-01") - date("2013-04-02")
Result (run with Zorba):

If any of the operands is a sequence of more than one item, an error is raised.
Example 5.39. Sequence of more than one number in an addition
(1, 2) + 3
Result (run with Zorba):
An error was raised: sequence of more than one item can not be promoted to parameter type xs:anyAtomicType? of function add()

If any of the operands is not a number, a date, a time or a duration, an error is raised.
Example 5.40. Null in an addition
1 + null
Result (run with Zorba):
An error was raised: arithmetic operation not defined between types "xs:integer" and "js:null"

If one of the operands evaluates to the empty sequence, then the operation results in the empty sequence.
Do not worry if the two operands do not have the same number type, JSONiq will do the adequate conversions.
Example 5.41. Basic arithmetic operations with an empty sequence
() + 2
Result (run with Zorba):