Skip to Content
Cypher ManualExpressionArithmetic Operators

Arithmetic Operator

NeuG supports common arithmetic operations including addition (+), subtraction (-), multiplication (*), division (/), and modulo (%).

Result Type

The data types that can participate in arithmetic operations are: INT32, UINT32, INT64, UINT64, FLOAT, DOUBLE. The result type of arithmetic operations is determined by the operand types, following these rules:

  • If the operands have different bit sizes (e.g. 32-bit vs 64-bit), the result uses the larger bit size
  • If the operands have the same bit size but different signs, the result promotes to the next larger signed type if available (e.g. INT32 + UINT32 -> INT64), otherwise to the unsigned type of the same size (e.g. INT64 + UINT64 -> UINT64)
  • Floating-point types take precedence over integer types, with DOUBLE being the highest precision floating-point type

The following table details the result types for different operand combinations:

Operand0Operand1Result
INT32INT32INT32
INT32UINT32INT64
INT32INT64INT64
INT32UINT64UINT64
INT32FLOATFLOAT
INT32DOUBLEDOUBLE
Operand0Operand1Result
UINT32INT32INT64
UINT32UINT32UINT32
UINT32INT64INT64
UINT32UINT64UINT64
UINT32FLOATFLOAT
UINT32DOUBLEDOUBLE
Operand0Operand1Result
INT64INT32INT64
INT64UINT32INT64
INT64INT64INT64
INT64UINT64UINT64
INT64FLOATFLOAT
INT64DOUBLEDOUBLE
Operand0Operand1Result
UINT64INT32UINT64
UINT64UINT32UINT64
UINT64INT64UINT64
UINT64UINT64UINT64
UINT64FLOATFLOAT
UINT64DOUBLEDOUBLE
Operand0Operand1Result
FLOATINT32FLOAT
FLOATUINT32FLOAT
FLOATINT64FLOAT
FLOATUINT64FLOAT
FLOATFLOATFLOAT
FLOATDOUBLEDOUBLE
Operand0Operand1Result
DOUBLEINT32DOUBLE
DOUBLEUINT32DOUBLE
DOUBLEINT64DOUBLE
DOUBLEUINT64DOUBLE
DOUBLEFLOATDOUBLE
DOUBLEDOUBLEDOUBLE

Error Handling

In addition to result types, arithmetic operations may encounter overflow, underflow, or divide-by-zero errors depending on the operand values. For these errors, NeuG provides different handling based on data type and deployment mode:

  1. Floating-point types: No special handling is performed; the system relies on standard IEEE 754 behavior returning Infinity, -Infinity, or NaN values.

  2. Integer types: Behavior differs between debug and release modes:

    • Debug mode: Lower performance requirements allow for input validation during execution with explicit exception throwing
    • Release mode: High performance requirements mean overflow/underflow returns undefined values, except for divide-by-zero which may cause explicit exception throwing

The following table details the error types that each operator may encounter:

OperatorOverflowUnderflowDivideByZeroExample
+YESYESN/AReturn CAST(2147483647, ‘int32’) + CAST(1, ‘int32’)
-YESYESN/AReturn CAST(-2147483648, ‘int32’) - CAST(1, ‘int32’)
*YESYESN/AReturn CAST(2147483647, ‘int32’) * CAST(2, ‘int32’)
/NONOYESReturn 5 / 0
%NONOYESReturn 5 % 0

Date Arithmetic

In addition to numeric types, arithmetic operations can also be performed on datetime and interval types. NeuG supports datetime arithmetic operations that allow you to add or subtract intervals from dates and timestamps, as well as calculate differences between temporal values.

Supported Operations

The following table details the supported date arithmetic operations:

OperationDescriptionExampleResult
DATE + INTERVALAdd interval to dateDATE('2011-02-15') + INTERVAL('5 DAYS')DATE('2011-02-20')
DATE - INTERVALSubtract interval from dateDATE('2011-02-15') - INTERVAL('5 DAYS')DATE('2011-02-10')
TIMESTAMP + INTERVALAdd interval to timestampTIMESTAMP('2011-10-21 14:25:13') + INTERVAL('30 HOURS 20 SECONDS')TIMESTAMP('2011-10-22 20:25:33')
TIMESTAMP - INTERVALSubtract interval from timestampTIMESTAMP('2011-10-21 14:25:13') - INTERVAL('30 HOURS 20 SECONDS')TIMESTAMP('2011-10-20 08:24:53')
INTERVAL + INTERVALAdd two intervalsINTERVAL('5 DAYS') + INTERVAL('30 HOURS 20 SECONDS')INTERVAL('6 DAYS 6 HOURS 20 SECONDS')
INTERVAL - INTERVALSubtract intervalsINTERVAL('5 DAYS') - INTERVAL('30 HOURS 20 SECONDS')INTERVAL('3 DAYS 17 HOURS 39 MINUTES 40 SECONDS')
TIMESTAMP - TIMESTAMPCalculate time differenceTIMESTAMP('2011-10-21 14:25:13') - TIMESTAMP('1989-10-21 14:25:13')INTERVAL('8035 DAYS')