2 9.3. Mathematical Functions and Operators #
4 Mathematical operators are provided for many PostgreSQL types. For
5 types without standard mathematical conventions (e.g., date/time types)
6 we describe the actual behavior in subsequent sections.
8 Table 9.4 shows the mathematical operators that are available for the
9 standard numeric types. Unless otherwise noted, operators shown as
10 accepting numeric_type are available for all the types smallint,
11 integer, bigint, numeric, real, and double precision. Operators shown
12 as accepting integral_type are available for the types smallint,
13 integer, and bigint. Except where noted, each form of an operator
14 returns the same data type as its argument(s). Calls involving multiple
15 argument data types, such as integer + numeric, are resolved by using
16 the type appearing later in these lists.
18 Table 9.4. Mathematical Operators
26 numeric_type + numeric_type → numeric_type
32 + numeric_type → numeric_type
34 Unary plus (no operation)
38 numeric_type - numeric_type → numeric_type
44 - numeric_type → numeric_type
50 numeric_type * numeric_type → numeric_type
56 numeric_type / numeric_type → numeric_type
58 Division (for integral types, division truncates the result towards
61 5.0 / 2 → 2.5000000000000000
67 numeric_type % numeric_type → numeric_type
69 Modulo (remainder); available for smallint, integer, bigint, and
74 numeric ^ numeric → numeric
76 double precision ^ double precision → double precision
82 Unlike typical mathematical practice, multiple uses of ^ will associate
83 left to right by default:
87 2 ^ (3 ^ 3) → 134217728
89 |/ double precision → double precision
95 ||/ double precision → double precision
101 @ numeric_type → numeric_type
107 integral_type & integral_type → integral_type
113 integral_type | integral_type → integral_type
119 integral_type # integral_type → integral_type
125 ~ integral_type → integral_type
131 integral_type << integer → integral_type
137 integral_type >> integer → integral_type
143 Table 9.5 shows the available mathematical functions. Many of these
144 functions are provided in multiple forms with different argument types.
145 Except where noted, any given form of a function returns the same data
146 type as its argument(s); cross-type cases are resolved in the same way
147 as explained above for operators. The functions working with double
148 precision data are mostly implemented on top of the host system's C
149 library; accuracy and behavior in boundary cases can therefore vary
150 depending on the host system.
152 Table 9.5. Mathematical Functions
160 abs ( numeric_type ) → numeric_type
166 cbrt ( double precision ) → double precision
172 ceil ( numeric ) → numeric
174 ceil ( double precision ) → double precision
176 Nearest integer greater than or equal to argument
182 ceiling ( numeric ) → numeric
184 ceiling ( double precision ) → double precision
186 Nearest integer greater than or equal to argument (same as ceil)
190 degrees ( double precision ) → double precision
192 Converts radians to degrees
194 degrees(0.5) → 28.64788975654116
196 div ( y numeric, x numeric ) → numeric
198 Integer quotient of y/x (truncates towards zero)
202 erf ( double precision ) → double precision
206 erf(1.0) → 0.8427007929497149
208 erfc ( double precision ) → double precision
210 Complementary error function (1 - erf(x), without loss of precision for
213 erfc(1.0) → 0.15729920705028513
215 exp ( numeric ) → numeric
217 exp ( double precision ) → double precision
219 Exponential (e raised to the given power)
221 exp(1.0) → 2.7182818284590452
223 factorial ( bigint ) → numeric
229 floor ( numeric ) → numeric
231 floor ( double precision ) → double precision
233 Nearest integer less than or equal to argument
239 gamma ( double precision ) → double precision
243 gamma(0.5) → 1.772453850905516
247 gcd ( numeric_type, numeric_type ) → numeric_type
249 Greatest common divisor (the largest positive number that divides both
250 inputs with no remainder); returns 0 if both inputs are zero; available
251 for integer, bigint, and numeric
255 lcm ( numeric_type, numeric_type ) → numeric_type
257 Least common multiple (the smallest strictly positive number that is an
258 integral multiple of both inputs); returns 0 if either input is zero;
259 available for integer, bigint, and numeric
261 lcm(1071, 462) → 23562
263 lgamma ( double precision ) → double precision
265 Natural logarithm of the absolute value of the gamma function
267 lgamma(1000) → 5905.220423209181
269 ln ( numeric ) → numeric
271 ln ( double precision ) → double precision
275 ln(2.0) → 0.6931471805599453
277 log ( numeric ) → numeric
279 log ( double precision ) → double precision
285 log10 ( numeric ) → numeric
287 log10 ( double precision ) → double precision
289 Base 10 logarithm (same as log)
293 log ( b numeric, x numeric ) → numeric
295 Logarithm of x to base b
297 log(2.0, 64.0) → 6.0000000000000000
299 min_scale ( numeric ) → integer
301 Minimum scale (number of fractional decimal digits) needed to represent
302 the supplied value precisely
304 min_scale(8.4100) → 2
306 mod ( y numeric_type, x numeric_type ) → numeric_type
308 Remainder of y/x; available for smallint, integer, bigint, and numeric
312 pi ( ) → double precision
314 Approximate value of π
316 pi() → 3.141592653589793
318 power ( a numeric, b numeric ) → numeric
320 power ( a double precision, b double precision ) → double precision
322 a raised to the power of b
326 radians ( double precision ) → double precision
328 Converts degrees to radians
330 radians(45.0) → 0.7853981633974483
332 round ( numeric ) → numeric
334 round ( double precision ) → double precision
336 Rounds to nearest integer. For numeric, ties are broken by rounding
337 away from zero. For double precision, the tie-breaking behavior is
338 platform dependent, but “round to nearest even” is the most common
343 round ( v numeric, s integer ) → numeric
345 Rounds v to s decimal places. Ties are broken by rounding away from
348 round(42.4382, 2) → 42.44
350 round(1234.56, -1) → 1230
352 scale ( numeric ) → integer
354 Scale of the argument (the number of decimal digits in the fractional
359 sign ( numeric ) → numeric
361 sign ( double precision ) → double precision
363 Sign of the argument (-1, 0, or +1)
367 sqrt ( numeric ) → numeric
369 sqrt ( double precision ) → double precision
373 sqrt(2) → 1.4142135623730951
375 trim_scale ( numeric ) → numeric
377 Reduces the value's scale (number of fractional decimal digits) by
378 removing trailing zeroes
380 trim_scale(8.4100) → 8.41
382 trunc ( numeric ) → numeric
384 trunc ( double precision ) → double precision
386 Truncates to integer (towards zero)
392 trunc ( v numeric, s integer ) → numeric
394 Truncates v to s decimal places
396 trunc(42.4382, 2) → 42.43
398 width_bucket ( operand numeric, low numeric, high numeric, count
401 width_bucket ( operand double precision, low double precision, high
402 double precision, count integer ) → integer
404 Returns the number of the bucket in which operand falls in a histogram
405 having count equal-width buckets spanning the range low to high. The
406 buckets have inclusive lower bounds and exclusive upper bounds. Returns
407 0 for an input less than low, or count+1 for an input greater than or
408 equal to high. If low > high, the behavior is mirror-reversed, with
409 bucket 1 now being the one just below low, and the inclusive bounds now
410 being on the upper side.
412 width_bucket(5.35, 0.024, 10.06, 5) → 3
414 width_bucket(9, 10, 0, 10) → 2
416 width_bucket ( operand anycompatible, thresholds anycompatiblearray ) →
419 Returns the number of the bucket in which operand falls given an array
420 listing the inclusive lower bounds of the buckets. Returns 0 for an
421 input less than the first lower bound. operand and the array elements
422 can be of any type having standard comparison operators. The thresholds
423 array must be sorted, smallest first, or unexpected results will be
426 width_bucket(now(), array['yesterday', 'today',
427 'tomorrow']::timestamptz[]) → 2
429 Table 9.6 shows functions for generating random numbers.
431 Table 9.6. Random Functions
439 random ( ) → double precision
441 Returns a random value in the range 0.0 <= x < 1.0
443 random() → 0.897124072839091
445 random ( min integer, max integer ) → integer
447 random ( min bigint, max bigint ) → bigint
449 random ( min numeric, max numeric ) → numeric
451 Returns a random value in the range min <= x <= max. For type numeric,
452 the result will have the same number of fractional decimal digits as
453 min or max, whichever has more.
457 random(-0.499, 0.499) → 0.347
459 random_normal ( [ mean double precision [, stddev double precision ]] )
462 Returns a random value from the normal distribution with the given
463 parameters; mean defaults to 0.0 and stddev defaults to 1.0
465 random_normal(0.0, 1.0) → 0.051285419
467 setseed ( double precision ) → void
469 Sets the seed for subsequent random() and random_normal() calls;
470 argument must be between -1.0 and 1.0, inclusive
474 The random() and random_normal() functions listed in Table 9.6 use a
475 deterministic pseudo-random number generator. It is fast but not
476 suitable for cryptographic applications; see the pgcrypto module for a
477 more secure alternative. If setseed() is called, the series of results
478 of subsequent calls to these functions in the current session can be
479 repeated by re-issuing setseed() with the same argument. Without any
480 prior setseed() call in the same session, the first call to any of
481 these functions obtains a seed from a platform-dependent source of
484 Table 9.7 shows the available trigonometric functions. Each of these
485 functions comes in two variants, one that measures angles in radians
486 and one that measures angles in degrees.
488 Table 9.7. Trigonometric Functions
496 acos ( double precision ) → double precision
498 Inverse cosine, result in radians
502 acosd ( double precision ) → double precision
504 Inverse cosine, result in degrees
508 asin ( double precision ) → double precision
510 Inverse sine, result in radians
512 asin(1) → 1.5707963267948966
514 asind ( double precision ) → double precision
516 Inverse sine, result in degrees
520 atan ( double precision ) → double precision
522 Inverse tangent, result in radians
524 atan(1) → 0.7853981633974483
526 atand ( double precision ) → double precision
528 Inverse tangent, result in degrees
532 atan2 ( y double precision, x double precision ) → double precision
534 Inverse tangent of y/x, result in radians
536 atan2(1, 0) → 1.5707963267948966
538 atan2d ( y double precision, x double precision ) → double precision
540 Inverse tangent of y/x, result in degrees
544 cos ( double precision ) → double precision
546 Cosine, argument in radians
550 cosd ( double precision ) → double precision
552 Cosine, argument in degrees
556 cot ( double precision ) → double precision
558 Cotangent, argument in radians
560 cot(0.5) → 1.830487721712452
562 cotd ( double precision ) → double precision
564 Cotangent, argument in degrees
568 sin ( double precision ) → double precision
570 Sine, argument in radians
572 sin(1) → 0.8414709848078965
574 sind ( double precision ) → double precision
576 Sine, argument in degrees
580 tan ( double precision ) → double precision
582 Tangent, argument in radians
584 tan(1) → 1.5574077246549023
586 tand ( double precision ) → double precision
588 Tangent, argument in degrees
594 Another way to work with angles measured in degrees is to use the unit
595 transformation functions radians() and degrees() shown earlier.
596 However, using the degree-based trigonometric functions is preferred,
597 as that way avoids round-off error for special cases such as sind(30).
599 Table 9.8 shows the available hyperbolic functions.
601 Table 9.8. Hyperbolic Functions
609 sinh ( double precision ) → double precision
613 sinh(1) → 1.1752011936438014
615 cosh ( double precision ) → double precision
621 tanh ( double precision ) → double precision
625 tanh(1) → 0.7615941559557649
627 asinh ( double precision ) → double precision
629 Inverse hyperbolic sine
631 asinh(1) → 0.881373587019543
633 acosh ( double precision ) → double precision
635 Inverse hyperbolic cosine
639 atanh ( double precision ) → double precision
641 Inverse hyperbolic tangent
643 atanh(0.5) → 0.5493061443340548