9.3. 數學函式及運算子
本節提供了 PostgreSQL 的數學運算方式。對於沒有標準數學約定的型別(例如,日期/時間型別),我們將在後續部分中介紹具體的行為。
Table 9.4 列出了可用的數學運算子。
Table 9.4. Mathematical Operators
Operator | Description | Example | Result | ||||
| addition |
|
| ||||
| subtraction |
|
| ||||
| multiplication |
|
| ||||
| division (integer division truncates the result) |
|
| ||||
| modulo (remainder) |
|
| ||||
| exponentiation (associates left to right) |
|
| ||||
` | /` | square root | ` | / 25.0` |
| ||
` | /` | cube root | ` | / 27.0` |
| ||
| factorial |
|
| ||||
| factorial (prefix operator) |
|
| ||||
| absolute value |
|
| ||||
| bitwise AND |
|
| ||||
` | ` | bitwise OR | `32 | 3` |
| ||
| bitwise XOR |
|
| ||||
| bitwise NOT |
|
| ||||
| bitwise shift left |
|
| ||||
| bitwise shift right |
|
|
位元運算子僅適用於整數資料型別,也可用於位元字串型別的位元和位元變化,如 Table 9.14 所示。
Table 9.5 列出了可用的數學函數。在該表中,dp 表示雙精確度。這些函數中的許多函數都提供了多種形式,且具有不同的參數型別。除非另有說明,否則函數的任何形式都將回傳與其參數相同的資料型別。使用雙精確度資料的功能主要以主機系統的 C 函式庫實作; 因此,邊界情況下的準確性和行為可能會因主機系統而有所差異。
Table 9.5. Mathematical Functions
Function | Return Type | Description | Example | Result |
| (same as input) | absolute value |
|
|
|
| cube root |
|
|
| (same as input) | nearest integer greater than or equal to argument |
|
|
| (same as input) | nearest integer greater than or equal to argument (same as |
|
|
|
| radians to degrees |
|
|
|
| integer quotient of |
|
|
| (same as input) | exponential |
|
|
| (same as input) | nearest integer less than or equal to argument |
|
|
| (same as input) | natural logarithm |
|
|
| (same as input) | base 10 logarithm |
|
|
| (same as input) | base 10 logarithm |
|
|
|
| logarithm to base |
|
|
| (same as argument types) | remainder of |
|
|
|
| “π” constant |
|
|
|
|
|
|
|
|
|
|
|
|
|
| degrees to radians |
|
|
| (same as input) | round to nearest integer |
|
|
|
| round to |
|
|
|
| scale of the argument (the number of decimal digits in the fractional part) |
|
|
| (same as input) | sign of the argument (-1, 0, +1) |
|
|
| (same as input) | square root |
|
|
| (same as input) | truncate toward zero |
|
|
|
| truncate to |
|
|
|
| return the bucket number to which |
|
|
|
| return the bucket number to which |
|
|
|
| return the bucket number to which |
|
|
Table 9.6 shows functions for generating random numbers.
Table 9.6. Random Functions
Function | Return Type | Description |
|
| random value in the range 0.0 <= x < 1.0 |
|
| set seed for subsequent |
The random()
function uses a simple linear congruential algorithm. It is fast but not suitable for cryptographic applications; see the pgcrypto module for a more secure alternative. If setseed()
is called, the results of subsequent random()
calls in the current session are repeatable by re-issuing setseed()
with the same argument.
Table 9.7 shows the available trigonometric functions. All these functions take arguments and return values of type double precision
. Each of the trigonometric functions comes in two variants, one that measures angles in radians and one that measures angles in degrees.
Table 9.7. Trigonometric Functions
Function (radians) | Function (degrees) | Description |
|
| inverse cosine |
|
| inverse sine |
|
| inverse tangent |
|
| inverse tangent of |
|
| cosine |
|
| cotangent |
|
| sine |
|
| tangent |
Note
Another way to work with angles measured in degrees is to use the unit transformation functions radians()
and degrees()
shown earlier. However, using the degree-based trigonometric functions is preferred, as that way avoids round-off error for special cases such as sind(30)
.
Table 9.8 shows the available hyperbolic functions. All these functions take arguments and return values of type double precision
.
Table 9.8. Hyperbolic Functions
Function | Description | Example | Result |
| hyperbolic sine |
|
|
| hyperbolic cosine |
|
|
| hyperbolic tangent |
|
|
| inverse hyperbolic sine |
|
|
| inverse hyperbolic cosine |
|
|
| inverse hyperbolic tangent |
|
|
Last updated