All Products
Search
Document Center

IoT Platform:Mathematical functions

Last Updated:Nov 29, 2023

This topic describes the syntax, parameters, and examples of mathematical functions supported by the data analysis feature. You can select mathematical functions based on your business requirements to compute data or convert data types.

FunctionDescription
ABSCalculates the absolute value.
ASINCalculates the arcsine.
COSCalculates the cosine.
COSHCalculates the hyperbolic cosine.
CEILRounds up a number and returns the nearest integer.
EXPCalculates the exponential value.
FLOORRounds down a number and returns the nearest integer.
LOGCalculates the logarithm.
POWCalculates the nth power of a value.
RANDReturns a random number.
ROUNDReturns a value rounded to the specified decimal place.
SINCalculates the sine.
SINHCalculates the hyperbolic sine.
SQRTCalculates the square root.
TANCalculates the tangent.
TANHCalculates the hyperbolic tangent.
TRUNCTruncates the input value to the specified decimal place.
HEXConverts an integer or a string into a hexadecimal number.
PICalculates the value of π.
UNHEXConverts a hexadecimal string into a string.

ABS

  • Syntax
    double abs(double <number>)
    bigint abs(bigint <number>)
    decimal abs(decimal <number>)
  • Description

    Calculates the absolute value of number.

  • Parameters
    number: required. If number is of the DOUBLE, BIGINT, or DECIMAL type, a value of the same type is returned.
    • If number is of the BIGINT type, the return value is of the BIGINT type.
    • If number is of the DOUBLE type, the return value is of the DOUBLE type.
    • If number is of the DECIMAL type, the return value is of the DECIMAL type.
    • If number is of the STRING type, it is implicitly converted into a value of the DOUBLE type before the calculation.
    • If the input value is set to null, null is returned.
    • If number is of a data type other than the preceding types, an error is returned.
    Note If the input value is of the BIGINT type and is greater than the maximum value of the BIGINT type, a value of the DOUBLE type is returned. However, the precision may be lost.
  • Return value

    The type of the return value depends on that of the input value, which can be DOUBLE, BIGINT, or DECIMAL.

  • Examples
    -- The return value is NULL. 
        abs(null);
    
    -- The return value is 1. 
        abs(-1);
    
    -- The return value is 1.2. 
        abs(-1.2);
    
    -- The return value is 2.0. 
        abs("-2");
    
    -- The value 1.2232083745629837E32 is returned. 
        abs(122320837456298376592387456923748);

ASIN

  • Syntax
    double asin(double <number>)
    decimal asin(decimal <number>)
  • Description

    Calculates the arcsine of number.

  • Parameters

    number: required. The value is of the DOUBLE or DECIMAL type and ranges from -1 to 1. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. The value ranges from -π/2 to π/2. If the input parameter is set to null, null is returned.

  • Examples
    --The return value is 1.5707963267948966. 
        asin(1);
    -- The return value is 1.5707963267948966. 
        asin(-1);

CEIL

  • Syntax
    bigint ceil(double <value>)
    bigint ceil(decimal <value>)
  • Description

    Rounds up value and returns the nearest integer.

  • Parameters

    value: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the BIGINT type is returned. If the input parameter is set to null, null is returned.

  • Examples
    -- The return value is 2. 
        ceil(1.1);
    -- The return value is -1. 
        ceil(-1.1);

COS

  • Syntax
    double cos(double <number>)
    decimal cos(decimal <number>)
  • Description

    Calculates the cosine of number, which is a radian value.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Examples
    -- The return value is 2.6794896585028633e-8. 
        cos(3.1415926/2);
    
    -- The return value is -0.9999999999999986. 
        cos(3.1415926);

COSH

  • Syntax
    double cosh(double <number>)
    decimal cosh(decimal <number>)
  • Description

    Calculates the hyperbolic cosine of number.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is 2.5091784169949913. 
        cosh(3.1415926/2);

EXP

  • Syntax
    double exp(double <number>)
    decimal exp(decimal <number>)
  • Description

    Calculates the exponential value of number.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is 4.810477252069109. 
        exp(3.1415926/2);

FLOOR

  • Syntax
    bigint floor(double <number>)
    bigint floor(decimal <number>)
  • Description

    Rounds down number and returns the nearest integer that is no greater than the value of number.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the BIGINT type is returned. If the input parameter is set to null, null is returned.

  • Examples
    -- The return value is 1. 
        floor(1.2);
    
    -- The return value is 0. 
        floor(0.1);
    
    -- The return value is -2. 
        floor(-1.2);
    
    -- The return value is -1. 
        floor(-0.1);
    
    -- The return value is 0. 
        floor(0.0);
    
    -- The return value is 0. 
        floor(-0.0);

LOG

  • Syntax
    double log(double <base>, double <x>)
    decimal log(decimal <base>, decimal <x>)
  • Description

    Calculates the logarithm of x whose base number is base.

  • Parameters
    • base: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.
    • x: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.
  • Return value
    The logarithm value of the DOUBLE or DECIMAL type is returned.
    • If an input parameter is set to null, null is returned.
    • If an input value is a negative value or 0, an error is returned.
    • If the value of base is 1, an error is returned. The value 1 causes division by zero.
  • Example
    -- The return value is 4.0. 
        log(2, 16);

POW

  • Syntax
    double pow(double <x>, double <y>)
    decimal pow(decimal <x>, decimal <y>)
  • Description

    Calculates the yth power of x, namely, x^y.

  • Parameters
    • x: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.
    • y: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.
  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If an input parameter is set to null, null is returned.

  • Example
    -- The return value is 65536.0. 
        pow(2, 16);

RAND

  • Syntax
    double rand(bigint <seed>)
  • Description

    Returns a random number of the DOUBLE type. The value ranges from 0 to 1.

  • Parameters

    seed: optional. The value is of the BIGINT type. This parameter specifies the random seed that determines the starting point in generating random numbers.

    Note You can use seed to determine the random number sequence. After seed is determined, the return value of this function is fixed.
  • Return value

    A value of the DOUBLE type is returned.

  • Examples
    -- The rerturn value is 4.7147460303803655E-4. 
        rand();
    -- The return value is 0.7308781907032909. 
        rand(1);

ROUND

  • Syntax
    double round(double <number>[, bigint <decimal_places>])
    decimal round(decimal <number>[, bigint <decimal_places>])
  • Description

    Returns a number rounded to the specified decimal place.

  • Parameters
    • number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.
    • decimal_places: optional. The value is a constant of the BIGINT type. The value is rounded to the specified decimal place. If it is of another data type, an error is returned. If this parameter is not specified, the number is rounded to the ones place. The default value is 0.
      Note The value of decimal_places can be negative. A negative value indicates counting from the decimal point to the left, and the decimal part is excluded. If decimal_places exceeds the length of the integer part, 0 is returned.
  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If an input parameter is set to null, null is returned.

  • Examples
    -- The return value is 125.0. 
        round(125.315);
    -- The return value is 125.3. 
        round(125.315, 1);
    -- The return value is 125.32. 
        round(125.315, 2);
    -- The return value is 125.315. 
        round(125.315, 3);
    -- The return value is -125.32. 
        round(-125.315, 2);
    -- The return value is 100.0. 
        round(123.345, -2);
    -- The return value is null. 
        round(null);
    -- The return value is 123.345. 
        round(123.345, 4);
    -- The return value is 0.0. 
        round(123.345, -4);

SIN

  • Syntax
    double sin(double <number>)
    decimal sin(decimal <number>)
  • Description

    Calculates the sine of number, which is a radian value.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is -0.3048106211022167. 
        sin(60);

SINH

  • Syntax
    double sinh(double <number>)
    decimal sinh(decimal <number>)
  • Description

    Calculates the hyperbolic sine of number.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is 5.343237290762231E12. 
        sinh(30);

SQRT

  • Syntax
    double sqrt(double <number>)
    decimal sqrt(decimal <number>)
  • Description

    Calculates the square root of number.

  • Parameters

    number: required. If the input value is of the DOUBLE or DECIMAL type, the value must be greater than 0. Otherwise, an exception is returned. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is 2.0. 
        sqrt(4);

TAN

  • Syntax
    double tan(double <number>)
    decimal tan(decimal <number>)
  • Description

    Calculates the tangent of number, which is a radian value.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is -6.405331196646276. 
        tan(30);

TANH

  • Syntax
    double tanh(double <number>)
    decimal tanh(decimal <number>)
  • Description

    Calculates the hyperbolic tangent of number.

  • Parameters

    number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If the input parameter is set to null, null is returned.

  • Example
    -- The return value is 1.0. 
    tanh(30);

TRUNC

  • Syntax
    double trunc(double <number>[, bigint <decimal_places>])
    decimal trunc(decimal <number>[, bigint <decimal_places>])
  • Description

    Truncates the input value of number to the specified decimal place.

  • Parameters
    • number: required. A value of the DOUBLE or DECIMAL type. If the input value is of the STRING or BIGINT type, it is implicitly converted into a value of the DOUBLE type before the calculation. If the input value is of another data type, an error is returned.
    • decimal_places: optional. The value is a constant of the BIGINT type. This parameter specifies the position where the number is truncated. If the value is of another data type, it is converted into a value of the BIGINT type. If this parameter is not specified, the number is truncated to the ones place.
      Note decimal_places can be a negative value, which indicates that the number is truncated from the decimal point to the left and the decimal part is left out. If decimal_places exceeds the length of the integer part, 0 is returned.
  • Return value

    A value of the DOUBLE or DECIMAL type is returned. If an input parameter is set to null, null is returned.

    Note
    • If a value of the DOUBLE type is returned, the return value may not be displayed properly. This issue exists in all systems. For more information, see trunc(125.815,1) in this example.
    • The number is filled with zeros from the specified position.
  • Examples
    -- The return value is 125.0. 
        trunc(125.815,0);
    -- The return value is 125.80000000000001. 
        trunc(125.815,1);
    -- The return value is 125.81. 
        trunc(125.815,2);
    -- The return value is 125.815. 
        trunc(125.815,3);
    -- The return value is -125.81. 
        trunc(-125.815,2);
    -- The return value is 120.0. 
        trunc(125.815,-1);
    -- The return value is 100.0. 
        trunc(125.815,-2);
    -- The return value is 0.0. 
        trunc(125.815,-3);
    -- The return value is 123.345. 
        trunc(123.345,4);
    -- The return value is 0.0. 
        trunc(123.345,-4);

HEX

  • Syntax
    string hex(bigint <number>) 
    string hex(string <number>)
    string hex(binary <number>)
  • Description

    Converts an integer or a string to a hexadecimal number.

  • Parameters

    number: required. If number is of the BIGINT type, a hexadecimal number is returned. If number is of the STRING type, a string in the hexadecimal format is returned.

  • Return value
    • If the input value is not 0 or null, a value of the STRING type is returned.
    • If the input value is 0, 0 is returned.
    • If the input value is set to null, an error is returned.
  • Examples
    --The return value is 0. 
        hex(0);
    -- The return value is 616263. 
        hex('abc');
    -- The return value is 11. 
        hex(17);
    -- The return value is 3137. 
        hex('17');
    -- An error is returned.  
        hex(null);

UNHEX

  • Syntax
    binary unhex(string <number>)
  • Description

    Converts a hexadecimal string into a string.

  • Parameters

    number: required. The value is a hexadecimal string.

  • Return value

    A value of the BINARY type is returned. If the input value is 0, an error is returned. If the input parameter is set to null, null is returned.

  • Examples
    --The return value is abc 
        unhex('616263');
    -- The return value is abc. 
        unhex(616263);

PI

  • Syntax
    double pi()
  • Description

    Calculates the value of π.

  • Return value

    A value of the DOUBLE type is returned.

  • Example
    -- The return value is 3.141592653589793. 
        pi();