Arithmetic Functions
Overview
Arithmetic functions perform mathematical operations on numeric types, including unary positive and negative signs, four arithmetic operations, modulo, exponentiation, square root, logarithm, exponent, trigonometric functions, rounding and truncation, sign determination, mathematical constants, random numbers, and base conversion.
For more information, see Arithmetic Functions.
Application Scenarios
- Numeric calculation: Performs addition, subtraction, multiplication, and division on columns in the SELECT statement to calculate service metrics.
- Precision control: Uses CEIL, FLOOR, ROUND, and TRUNCATE to convert and truncate the numeric precision.
- Scientific calculation: Performs scientific and engineering calculations using trigonometric functions, logarithms, and exponential functions.
- Data conversion: Converts values between number systems and strings using BIN and HEX.
- Random sampling: Uses RAND to generate random numbers for data sampling or test data construction.
Notes and Constraints
- Integer division truncates the fractional part. To retain the fractional part, use the DECIMAL type or divide the value by a DECIMAL value.
- Division by zero produces a NULL result.
- The input and return types of POWER and SQRT are DOUBLE.
- The input of LN, LOG10, and LOG2 must be greater than 0. Otherwise, NULL is returned.
- The parameters of the trigonometric functions SIN, COS, TAN, and COT are radians instead of degrees. The input ranges of the inverse trigonometric functions ASIN and ACOS are [-1, 1]. If the input is out of the range, NULL is returned.
- If any input of an arithmetic function is NULL, the result is NULL.
- TRUNCATE directly truncates the value instead of rounding it, which is different from the rounding behavior of ROUND.
Syntax Structure
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| + numeric | Same as the input | Unary plus sign. Returns the value of numeric itself without changing the value. The numeric type can be TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, or DECIMAL. | SELECT +5; -- 5 |
| - numeric | Same as the input | Unary minus sign. Returns the negation of numeric. The numeric type can be any numeric type. | SELECT -5; -- -5 |
| numeric1 + numeric2 | Automatic type promotion | Addition operation. Returns the sum of numeric1 and numeric2. The two operands are of any numeric type, and the result type is automatically promoted according to the SQL standard (for example, INT + DOUBLE → DOUBLE). | SELECT 10 + 5; -- 15 |
| numeric1 - numeric2 | Automatic type promotion | Subtraction operation. Returns the difference between numeric1 and numeric2. The two operands are of any numeric type, and the result type is automatically promoted. | SELECT 10 - 5; -- 5 |
| numeric1 * numeric2 | Automatic type promotion | Multiplication operation. Returns the product of numeric1 and numeric2. The two operands are of any numeric type, and the result type is automatically promoted. | SELECT 10 * 5; -- 50 |
| numeric1 / numeric2 | Automatic type promotion | Division operation. Returns the quotient of numeric1 divided by numeric2. For integer division, the fractional part is truncated. To retain the fractional part, use the DECIMAL type. If the divisor is zero, NULL is returned. | SELECT 10 / 3; -- 3 |
| POWER(numeric1, numeric2) | DOUBLE | Exponentiation operation. Returns the numeric2th power of numeric1, that is, numeric1^numeric2. If both parameters are of the numeric type, DOUBLE is returned. If numeric1 is a negative number and numeric2 is not an integer, the result is NULL. | SELECT POWER(2, 3); -- 8.0 |
| ABS(numeric) | Same as the input | Absolute value. Returns the absolute value of numeric. If numeric is of any numeric type, the return type is the same as the input. | SELECT ABS(-5); -- 5 |
| MOD(numeric1, numeric2) | Same as the input | Modulo operation. Returns the remainder of numeric1 divided by numeric2, which is equivalent to the % operator. If numeric2 is 0, NULL is returned. If both parameters are of the integer type, the return value is an integer. Otherwise, the return value is of the DECIMAL type. | SELECT MOD(10, 3); -- 1 |
| SQRT(numeric) | DOUBLE | Square root. Returns the non-negative square root of numeric. If numeric is a negative number, NULL is returned. | SELECT SQRT(16); -- 4.0 |
| LN(numeric) | DOUBLE | Natural logarithm. Returns the natural logarithm of numeric (base e). The value of numeric must be greater than 0. Otherwise, NULL is returned. | SELECT LN(2.718281828); -- 0.9999999995 |
| LOG10(numeric) | DOUBLE | Base-10 logarithm. Returns the base-10 logarithm of numeric. The value of numeric must be greater than 0. Otherwise, NULL is returned. | SELECT LOG10(100); -- 2.0 |
| LOG2(numeric) | DOUBLE | Base-2 logarithm. Returns the base-2 logarithm of numeric. The value of numeric must be greater than 0. Otherwise, NULL is returned. | SELECT LOG2(8); -- 3.0 |
| LOG(numeric1, numeric2) | DOUBLE | Custom-base logarithm. Returns the logarithm of numeric1 with numeric2 as the base. The value of numeric1 must be greater than 0, and the value of numeric2 must be greater than 0 and not equal to 1. Otherwise, NULL is returned. | SELECT LOG(8, 2); -- 3.0 |
| EXP(numeric) | DOUBLE | Exponential operation. Returns the numeric power of e (the natural constant), that is, e^numeric. It is the inverse function of LN. | SELECT EXP(1); -- 2.718281828459045 |
| CEIL(numeric) / CEILING(numeric) | Same as the input | Ceiling. Returns the smallest integer greater than or equal to numeric. The numeric value can be of any numeric type. | SELECT CEIL(3.2); -- 4 |
| FLOOR(numeric) | Same as the input | Floor. Returns the largest integer less than or equal to numeric. The numeric value can be of any numeric type. | SELECT FLOOR(3.8); -- 3 |
| ROUND(numeric, integer) | Same as the input | Rounding. Rounds numeric to integer decimal places. If integer is positive, the specified number of decimal places is retained. If integer is negative, the integer part is rounded. If integer is 0, the number is rounded to the nearest integer. | SELECT ROUND(3.456, 2); -- 3.46 |
| TRUNCATE(numeric, integer) | Same as the input | Truncation. Truncates the numeric value to the specified number of integer decimal places. The redundant decimal places are discarded without rounding. If the integer part is positive, the corresponding decimal places are retained. If the integer part is negative, the integer part is truncated. If the integer part is 0, the value is truncated to an integer. | SELECT TRUNCATE(3.456, 2); -- 3.45 |
| SIGN(numeric) | INT | Sign function. Returns the sign of a numeric value. If the numeric value is greater than 0, 1 is returned. If the numeric value is equal to 0, 0 is returned. If the numeric value is less than 0, -1 is returned. | SELECT SIGN(-5); -- -1 |
| SIN(numeric) | DOUBLE | Sine function. Returns the sine value of numeric (in radians), where numeric is a numeric type indicating radians. | SELECT SIN(0); -- 0.0 |
| COS(numeric) | DOUBLE | Cosine function. Returns the cosine value of numeric (in radians), where numeric is a numeric type indicating radians. | SELECT COS(0); -- 1.0 |
| TAN(numeric) | DOUBLE | Tangent function. Returns the tangent value of numeric (in radians), where numeric is a numeric type indicating radians. | SELECT TAN(0); -- 0.0 |
| COT(numeric) | DOUBLE | Cotangent function. Returns the cotangent value of numeric (in radians), that is, 1/TAN(numeric). If numeric is 0 or an integer multiple of π, NULL is returned. | SELECT COT(1); -- 0.6420926159343308 |
| ASIN(numeric) | DOUBLE | Arcsine function. Returns the arcsine value of numeric (in radians). The value of numeric must be in the range of [-1, 1]. If the value is out of the range, NULL is returned. | SELECT ASIN(1); -- 1.5707963267948966 |
| ACOS(numeric) | DOUBLE | Arc cosine function. Returns the arc cosine value of numeric (in radians). The value of numeric must be in the range of [-1, 1]. If the value is out of the range, NULL is returned. | SELECT ACOS(1); -- 0.0 |
| ATAN(numeric) | DOUBLE | Arc tangent function. Returns the arc tangent of numeric (in radians). The numeric value can be of any numeric type, and the return value range is (-π/2, π/2). | SELECT ATAN(1); -- 0.7853981633974483 |
| ATAN2(numeric1, numeric2) | DOUBLE | Two-argument arc tangent function. Returns the angle (in radians) between the point (numeric2, numeric1) and the positive x-axis. Unlike ATAN, ATAN2 determines the quadrant based on the two arguments, and the return value range is (-π, π]. | SELECT ATAN2(1, 1); -- 0.7853981633974483 |
| PI() | DOUBLE | Pi function. Returns the approximate value of π, which is 3.141592653589793. This function takes no parameters. | SELECT PI(); -- 3.141592653589793 |
| E() | DOUBLE | Natural constant. Returns the approximate value of e, 2.718281828459045. No parameter is required. | SELECT E(); -- 2.718281828459045 |
| RAND() | DOUBLE | Random number. Returns a pseudo-random DOUBLE value in the range of [0.0, 1.0). No parameter is required. Each time the function is called, a different value is returned. | SELECT RAND(); -- 0.123... (random value) |
| RAND(integer) | DOUBLE | Seed random number. Returns a pseudo-random DOUBLE value in the range of [0.0, 1.0). The integer is the seed value. The same seed generates the same random sequence. | SELECT RAND(1); -- 0.730... (fixed seed) |
| BIN(integer) | STRING | Converts an integer to a binary string. Returns the binary representation of the integer. The integer is of an integer type (TINYINT, SMALLINT, INT, or BIGINT). If the integer is a negative number, the binary representation of its two's complement is returned. | SELECT BIN(5); -- '101' |
| HEX(numeric) | STRING | Converts a value to a hexadecimal string. Returns the hexadecimal representation of a numeric value. If the numeric value is of the integer type, its hexadecimal representation is returned. If the numeric value is of the STRING type, each character is converted to a two-digit hexadecimal number. | SELECT HEX(255); -- 'FF' |
| BITAND | INT | The BITAND function performs a bitwise AND operation. The bitwise AND operation compares the binary bits of two integers bit by bit. If both corresponding bits are 1, the result bit is 1. Otherwise, the result bit is 0. | For details, see BITAND. |
| BITNOT | INT | The BITNOT function performs a bitwise NOT operation. The bitwise NOT operation inverts the binary bits of an integer, that is, 0 becomes 1 and 1 becomes 0. Because the INT type uses two's complement representation, the result after inversion must be understood according to the two's complement rules. | For details, see BITNOT. |
| BITOR | INT | The BITOR function is used to perform the bitwise OR operation. The bitwise OR operation compares the binary bits of two integers bit by bit. If at least one of the two corresponding bits is 1, the result bit is 1. Otherwise, the result bit is 0. | For details, see BITOR. |
| BITXOR | INT | The BITXOR function is used to perform the bitwise XOR operation. The bitwise XOR operation compares the binary bits of two integers bit by bit. If the two corresponding bits are different, the result bit is 1. If they are the same, the result bit is 0. | For details, see BITXOR. |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot