This section is from the "Practical PostgreSQL" book, by John Worsley and Joshua Drake. Also available from Amazon: Practical PostgreSQL.
Table 4-14. Mathematical Functions in PostgreSQL
Function | Return Type | Description | ||
|---|---|---|---|---|
abs (x) | (same as argument type) | absolute value | ||
cbrt (double precision) | double precision | cube root | ||
ceil (numeric) | numeric | smallest integer not less than argument | ||
degrees (double precision) | double precision | convert radians to degrees | ||
exp (double precision) | double precision | exponential function | ||
floor (numeric) | numeric | largest integer not greater than argument | ||
ln (double precision) | double precision | natural logarithm | ||
log (double precision) | double precision | base 10 algorithm | ||
log (base numeric, x numeric) | numeric | logarithm to specified base | ||
mod (y, x) | (same as argument types) | remainder (modulo) of the division y/x | ||
pi () | double precision | "Pi" constant | ||
pow (double precision, double precision) | double precision | raise number to the specified exponent | ||
radians (double precision) | double precision | convert degrees to radians | ||
random () | double precision | a pseudo-random value between 0.0 to 1.0 | ||
round (double precision) | double precision | round to nearest integer | ||
round (value numeric, scale integer) | numeric | round to specified number of decimal places | ||
sqrt (double precision) | double precision | square root | ||
trunc (double precision) | double precision | truncate (toward zero) | ||
trunc (value numeric, scale integer) | numeric | truncate to specified number of decimal places |
Example 4-45. Using the random() and round() Functions
The following SQL statement generates a random number from 0.0 to 1.0 with the random() function, which is multiplied by an integer constant of 53, and rounded to the nearest integer.
booktown=# SELECT round(53 * random());
round
-------
36
(1 row)
booktown=# SELECT round(53 * random());
round
-------
11
(1 row)
As you can see in Example 4-45 , the first time the statement is executed, the returned value is 36. The second time the statement is executed, it returns 11. As you might expect, the results of random() are totally unpredictable. round() , on the other hand, always rounds a numeric value to the nearest whole integer.
Table 4-15. Trigonometric Functions
Function | Description |
|---|---|
acos(double precision x) | Returns the inverse cosine of the passed double precision value x |
asin(double precision x) | Returns the inverse sine of the passed double precision value x |
atan(double precision x) | Returns the inverse tangent of the passed double precision value x |
atan2(double precision x,double precision y) | Returns the inverse tangent of the quotient of double precision values ( y divided by x ) |
cos(double precision x) | Returns the cosine of the double precision value x |
cot(double precision x) | Returns the cotangent of the double precision value x |
sin(double precision x) | Returns the sine of the double precision value x |
tan(double precision x) | Returns the tangent of the double precision value x |
 
Continue to: