Arithmetic operators
Arithmetic operators work with numbers and also provide checked Duration and DateTime operations. The result type depends on the operand pair.
FQL supports the following arithmetic operators:
+addition-subtraction*multiplication/division%modulus
Unary plus and unary minus are supported as well:
For exponentiation, there is a numeric function POW(). The syntax base ** exp is not supported.
Some example arithmetic operations:
Type conversion
Arithmetic operators accept operands of any type. The conversion rules depend on the operator.
Addition
The + operator performs numeric addition when both operands are numbers. When either operand is a string, it normally performs string concatenation instead. Temporal operands take precedence: a Duration or DateTime uses the temporal rules below before string concatenation is considered.
Subtraction, multiplication, division, and modulus
The -, *, /, and % operators always convert their operands to numbers. The conversion rules are:
NONEis converted to0.falseis converted to0,trueis converted to1.- A valid numeric value remains unchanged.
- String values are converted to a number if they contain a valid numeric representation. Strings with non-numeric contents are converted to
0. - An empty array is converted to
0. A non-empty array is converted by summing the numeric values of all its elements. - Objects, binary, and custom types are converted to
0.
Temporal arithmetic
Duration arithmetic converts operands only where the table below says “coercible Duration.” Numeric Duration inputs are milliseconds, duration strings may be compound, and fractional nanoseconds are truncated toward zero. See Basic Types for the complete conversion table.
| Expression | Result |
|---|---|
Duration + coercible Duration |
Duration |
coercible Duration + Duration |
Duration |
Duration - coercible Duration |
Duration |
Duration * number or numeric string |
scaled Duration |
number or numeric string * Duration |
scaled Duration |
Duration / number or numeric string |
scaled Duration |
Duration / Duration or duration string |
Int for an exact ratio, otherwise Float |
DateTime + coercible Duration |
DateTime |
coercible Duration + DateTime |
DateTime |
DateTime - coercible Duration |
DateTime |
DateTime - DateTime |
elapsed Duration |
DateTime - RFC3339 string |
elapsed Duration |
For DateTime - String, Ferret first tries to parse the string as an RFC3339 DateTime, then as a Duration. If neither conversion succeeds, the expression raises a runtime error.
The following pairs are unsupported and raise an invalid-operation error:
DateTime + DateTimeDuration - DateTime- DateTime multiplication, division, or modulus
- Duration multiplication by another Duration or a duration-form string
- reverse division such as
2 / 1s - Duration modulus
All Duration and DateTime arithmetic is checked for overflow. DateTime arithmetic uses canonical instants and does not retain monotonic clock metadata.