Skip to main content

Arithmetic Operators and Functions

About 3 min

Arithmetic Operators and Functions

Arithmetic Operators

Unary Arithmetic Operators

Supported operators: +, -

Supported input data types: INT32, INT64 and FLOAT

Output data type: consistent with the input data type

Binary Arithmetic Operators

Supported operators: +, -, *, /, %

Supported input data types: INT32, INT64, FLOAT and DOUBLE

Output data type: DOUBLE

Note: Only when the left operand and the right operand under a certain timestamp are not null, the binary arithmetic operation will have an output value.

Example

select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1

Result:

+-----------------------------+-------------+--------------+-------------+-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
|                         Time|root.sg.d1.s1|-root.sg.d1.s1|root.sg.d1.s2|root.sg.d1.s2|root.sg.d1.s1 + root.sg.d1.s2|root.sg.d1.s1 - root.sg.d1.s2|root.sg.d1.s1 * root.sg.d1.s2|root.sg.d1.s1 / root.sg.d1.s2|root.sg.d1.s1 % root.sg.d1.s2|
+-----------------------------+-------------+--------------+-------------+-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
|1970-01-01T08:00:00.001+08:00|          1.0|          -1.0|          1.0|          1.0|                          2.0|                          0.0|                          1.0|                          1.0|                          0.0|
|1970-01-01T08:00:00.002+08:00|          2.0|          -2.0|          2.0|          2.0|                          4.0|                          0.0|                          4.0|                          1.0|                          0.0|
|1970-01-01T08:00:00.003+08:00|          3.0|          -3.0|          3.0|          3.0|                          6.0|                          0.0|                          9.0|                          1.0|                          0.0|
|1970-01-01T08:00:00.004+08:00|          4.0|          -4.0|          4.0|          4.0|                          8.0|                          0.0|                         16.0|                          1.0|                          0.0|
|1970-01-01T08:00:00.005+08:00|          5.0|          -5.0|          5.0|          5.0|                         10.0|                          0.0|                         25.0|                          1.0|                          0.0|
+-----------------------------+-------------+--------------+-------------+-------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
Total line number = 5
It costs 0.014s

Arithmetic Functions

Currently, IoTDB supports the following mathematical functions. The behavior of these mathematical functions is consistent with the behavior of these functions in the Java Math standard library.

Function NameAllowed Input Series Data TypesOutput Series Data TypeRequired AttributesCorresponding Implementation in the Java Standard Library
SININT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#sin(double)
COSINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#cos(double)
TANINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#tan(double)
ASININT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#asin(double)
ACOSINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#acos(double)
ATANINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#atan(double)
SINHINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#sinh(double)
COSHINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#cosh(double)
TANHINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#tanh(double)
DEGREESINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#toDegrees(double)
RADIANSINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#toRadians(double)
ABSINT32 / INT64 / FLOAT / DOUBLESame type as the input series/Math#abs(int) / Math#abs(long) /Math#abs(float) /Math#abs(double)
SIGNINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#signum(double)
CEILINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#ceil(double)
FLOORINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#floor(double)
ROUNDINT32 / INT64 / FLOAT / DOUBLEDOUBLE'places' : Round the significant number, positive number is the significant number after the decimal point, negative number is the significant number of whole numberMath#rint(Math#pow(10,places))/Math#pow(10,places)
EXPINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#exp(double)
LNINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#log(double)
LOG10INT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#log10(double)
SQRTINT32 / INT64 / FLOAT / DOUBLEDOUBLE/Math#sqrt(double)

Example:

select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000;

Result:

+-----------------------------+-------------------+-------------------+--------------------+-------------------+
|                         Time|     root.sg1.d1.s1|sin(root.sg1.d1.s1)| cos(root.sg1.d1.s1)|tan(root.sg1.d1.s1)|
+-----------------------------+-------------------+-------------------+--------------------+-------------------+
|2020-12-10T17:11:49.037+08:00|7360723084922759782| 0.8133527237573284|  0.5817708713544664| 1.3980636773094157|
|2020-12-10T17:11:49.038+08:00|4377791063319964531|-0.8938962705202537|  0.4482738644511651| -1.994085181866842|
|2020-12-10T17:11:49.039+08:00|7972485567734642915| 0.9627757585308978|-0.27030138509681073|-3.5618602479083545|
|2020-12-10T17:11:49.040+08:00|2508858212791964081|-0.6073417341629443| -0.7944406950452296| 0.7644897069734913|
|2020-12-10T17:11:49.041+08:00|2817297431185141819|-0.8419358900502509| -0.5395775727782725| 1.5603611649667768|
+-----------------------------+-------------------+-------------------+--------------------+-------------------+
Total line number = 5
It costs 0.008s

ROUND

Example:

select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1 
+-----------------------------+-------------+--------------------+----------------------+-----------------------+
|                         Time|root.db.d1.s4|ROUND(root.db.d1.s4)|ROUND(root.db.d1.s4,2)|ROUND(root.db.d1.s4,-1)|
+-----------------------------+-------------+--------------------+----------------------+-----------------------+
|1970-01-01T08:00:00.001+08:00|    101.14345|               101.0|                101.14|                  100.0|
|1970-01-01T08:00:00.002+08:00|    20.144346|                20.0|                 20.14|                   20.0|
|1970-01-01T08:00:00.003+08:00|    20.614372|                21.0|                 20.61|                   20.0|
|1970-01-01T08:00:00.005+08:00|    20.814346|                21.0|                 20.81|                   20.0|
|1970-01-01T08:00:00.006+08:00|     60.71443|                61.0|                 60.71|                   60.0|
|2023-03-13T16:16:19.764+08:00|    10.143425|                10.0|                 10.14|                   10.0|
+-----------------------------+-------------+--------------------+----------------------+-----------------------+
Total line number = 6
It costs 0.059s

Copyright © 2023 The Apache Software Foundation.
Apache and the Apache feather logo are trademarks of The Apache Software Foundation

Have a question? Connect with us on QQ, WeChat, or Slack. Join the community now.

We use Google Analytics to collect anonymous, aggregated usage information.