バージョンごとのドキュメント一覧

9.3. 算術関数と演算子 #

<title>Mathematical Functions and Operators</title>

Mathematical operators are provided for many <productname>PostgreSQL</productname> types. For types without standard mathematical conventions (e.g., date/time types) we describe the actual behavior in subsequent sections. PostgreSQLの数多くの型に対する算術演算子が用意されています。 標準算術表現法が存在しない型(例えば、日付/時刻データ型)については、後続する節で実際の動作を説明します。

<xref linkend="functions-math-op-table"/> shows the mathematical operators that are available for the standard numeric types. Unless otherwise noted, operators shown as accepting <replaceable>numeric_type</replaceable> are available for all the types <type>smallint</type>, <type>integer</type>, <type>bigint</type>, <type>numeric</type>, <type>real</type>, and <type>double precision</type>. Operators shown as accepting <replaceable>integral_type</replaceable> are available for the types <type>smallint</type>, <type>integer</type>, and <type>bigint</type>. Except where noted, each form of an operator returns the same data type as its argument(s). Calls involving multiple argument data types, such as <type>integer</type> <literal>+</literal> <type>numeric</type>, are resolved by using the type appearing later in these lists. 表 9.4は標準の数値型で使用可能な算術演算子を示しています。 特に説明がない限り、numeric_typeを受け付けると表示されている演算子はすべてのsmallintintegerbigintnumericrealdouble precisionデータ型で利用可能です。 integral_typeを受け付けると表示されている演算子はすべてのsmallintintegerbigintデータ型で利用可能です。 特に説明がない限り、それぞれの演算子は引数と同じデータ型を返します。 integer + numericのように、複数の引数データ型が使われる呼び出しは、このリストの後で現れる型を使って解決されます。

表9.4 算術演算子

<title>Mathematical Operators</title>

Operator 演算子

Description 説明

Example(s)

numeric_type + numeric_typenumeric_type

Addition

2 + 35

+ numeric_typenumeric_type

Unary plus (no operation) 単項和(演算なし)

+ 3.53.5

numeric_type - numeric_typenumeric_type

Subtraction

2 - 3-1

- numeric_typenumeric_type

Negation 否定

- (-4)4

numeric_type * numeric_typenumeric_type

Multiplication

2 * 36

numeric_type / numeric_typenumeric_type

Division (for integral types, division truncates the result towards zero) 商(整数型では、除算によってゼロへ余りが切り捨てられます)

5.0 / 22.5000000000000000

5 / 22

(-5) / 2-2

numeric_type % numeric_typenumeric_type

Modulo (remainder); available for <type>smallint</type>, <type>integer</type>, <type>bigint</type>, and <type>numeric</type> 剰余(余り)。smallintintegerbigintnumericで利用可能

5 % 41

numeric ^ numericnumeric

double precision ^ double precisiondouble precision

Exponentiation 累乗

2 ^ 38

Unlike typical mathematical practice, multiple uses of <literal>^</literal> will associate left to right by default: 典型的な数学的用法とは違って、デフォルトでは^は左から右に適用されます。

2 ^ 3 ^ 3512

2 ^ (3 ^ 3)134217728

|/ double precisiondouble precision

Square root 平方根

|/ 25.05

||/ double precisiondouble precision

Cube root 立方根

||/ 64.04

@ numeric_typenumeric_type

Absolute value 絶対値

@ -5.05.0

integral_type & integral_typeintegral_type

Bitwise AND ビットごとのAND

91 & 1511

integral_type | integral_typeintegral_type

Bitwise OR ビットごとのOR

32 | 335

integral_type # integral_typeintegral_type

Bitwise exclusive OR ビットごとの排他的論理和

17 # 520

~ integral_typeintegral_type

Bitwise NOT ビットごとのNOT

~1-2

integral_type << integerintegral_type

Bitwise shift left ビットごとの左シフト

1 << 416

integral_type >> integerintegral_type

Bitwise shift right ビットごとの右シフト

8 >> 22


<xref linkend="functions-math-func-table"/> shows the available mathematical functions. Many of these functions are provided in multiple forms with different argument types. Except where noted, any given form of a function returns the same data type as its argument(s); cross-type cases are resolved in the same way as explained above for operators. The functions working with <type>double precision</type> data are mostly implemented on top of the host system's C library; accuracy and behavior in boundary cases can therefore vary depending on the host system. 表 9.5に使用可能な算術関数を示します。 これら関数の多くは、異なる引数型を持つ複数の形で提供されています。 特に記述がある場合を除き、すべての形式の関数はその引数と同じデータ型を返します。 複数の型をまたがる場合は上記の演算子のところで説明したのと同じ方法で解決されます。 double precisionデータに対する関数のほとんどはホストシステムのCライブラリの上層に実装されています。このため、精度と境界近くの場合の振舞いはホストシステムに依存して変わります。

表9.5 算術関数

<title>Mathematical Functions</title>

Function 関数

Description 説明

Example(s)

abs ( numeric_type ) → numeric_type

Absolute value 絶対値

abs(-17.4)17.4

cbrt ( double precision ) → double precision

Cube root 立方根

cbrt(64.0)4

ceil ( numeric ) → numeric

ceil ( double precision ) → double precision

Nearest integer greater than or equal to argument 引数より大きいか等しく、引数に最も近い整数

ceil(42.2)43

ceil(-42.8)-42

ceiling ( numeric ) → numeric

ceiling ( double precision ) → double precision

Nearest integer greater than or equal to argument (same as <function>ceil</function>) 引数より大きいか等しく、引数に最も近い整数(ceilと同じ)

ceiling(95.3)96

degrees ( double precision ) → double precision

Converts radians to degrees ラディアンを度に変換

degrees(0.5)28.64788975654116

div ( y numeric, x numeric ) → numeric

Integer quotient of <parameter>y</parameter>/<parameter>x</parameter> (truncates towards zero) y/xの整数商 (0に向かって切り捨て)

div(9, 4)2

erf ( double precision ) → double precision

Error function 誤差関数

erf(1.0)0.8427007929497149

erfc ( double precision ) → double precision

Complementary error function (<literal>1 - erf(x)</literal>, without loss of precision for large inputs) 相補誤差関数(1-erf(x)、大きな入力における精度の損失なし)

erfc(1.0)0.15729920705028513

exp ( numeric ) → numeric

exp ( double precision ) → double precision

Exponential (<literal>e</literal> raised to the given power) 指数(eを底とする指定のべき乗)

exp(1.0)2.7182818284590452

factorial ( bigint ) → numeric

Factorial 階乗

factorial(5)120

floor ( numeric ) → numeric

floor ( double precision ) → double precision

Nearest integer less than or equal to argument 引数より小さいか等しく、引数に最も近い整数

floor(42.8)42

floor(-42.8)-43

gcd ( numeric_type, numeric_type ) → numeric_type

Greatest common divisor (the largest positive number that divides both inputs with no remainder); returns <literal>0</literal> if both inputs are zero; available for <type>integer</type>, <type>bigint</type>, and <type>numeric</type> 最大公約数(余りなく入力を割る最大の正の整数)。 両方の入力が0なら0を返す。 integerbigintnumericで利用可能

gcd(1071, 462)21

lcm ( numeric_type, numeric_type ) → numeric_type

Least common multiple (the smallest strictly positive number that is an integral multiple of both inputs); returns <literal>0</literal> if either input is zero; available for <type>integer</type>, <type>bigint</type>, and <type>numeric</type> 最小公倍数(両方の入力の整数倍となる最小の厳密な正の数)。 両方の入力が0なら0を返す。 integerbigintnumericで利用可能

lcm(1071, 462)23562

ln ( numeric ) → numeric

ln ( double precision ) → double precision

Natural logarithm 自然対数

ln(2.0)0.6931471805599453

log ( numeric ) → numeric

log ( double precision ) → double precision

Base 10 logarithm 10を底とした対数

log(100)2

log10 ( numeric ) → numeric

log10 ( double precision ) → double precision

Base 10 logarithm (same as <function>log</function>) 10を底とした対数(logと同じ)

log10(1000)3

log ( b numeric, x numeric ) → numeric

Logarithm of <parameter>x</parameter> to base <parameter>b</parameter> bを底としたxの対数

log(2.0, 64.0)6.0000000000000000

min_scale ( numeric ) → integer

Minimum scale (number of fractional decimal digits) needed to represent the supplied value precisely 与えられた値を正確に表現するのに必要な最小の桁数(小数点以下の10進の桁数)

min_scale(8.4100)2

mod ( y numeric_type, x numeric_type ) → numeric_type

Remainder of <parameter>y</parameter>/<parameter>x</parameter>; available for <type>smallint</type>, <type>integer</type>, <type>bigint</type>, and <type>numeric</type> y/xの剰余。 smallintintegerbigintnumericで利用可能

mod(9, 4)1

pi ( ) → double precision

Approximate value of <phrase role="symbol_font">&pi;</phrase> πの近似値

pi()3.141592653589793

power ( a numeric, b numeric ) → numeric

power ( a double precision, b double precision ) → double precision

<parameter>a</parameter> raised to the power of <parameter>b</parameter> ab

power(9, 3)729

radians ( double precision ) → double precision

Converts degrees to radians 度をラディアンに変換

radians(45.0)0.7853981633974483

round ( numeric ) → numeric

round ( double precision ) → double precision

Rounds to nearest integer. For <type>numeric</type>, ties are broken by rounding away from zero. For <type>double precision</type>, the tie-breaking behavior is platform dependent, but <quote>round to nearest even</quote> is the most common rule. 最も近い整数へ丸めます。 numericの場合、小数点以下を四捨五入します。 double precisionでは端数処理の振る舞いはプラットフォーム依存です。 しかし、最も普通の規則は最近接偶数への丸め(round to nearest even)です。

round(42.4)42

round ( v numeric, s integer ) → numeric

Rounds <parameter>v</parameter> to <parameter>s</parameter> decimal places. Ties are broken by rounding away from zero. vを小数点第s位まで丸めます。 小数点以下を切り上げて端数を処理します。

round(42.4382, 2)42.44

round(1234.56, -1)1230

scale ( numeric ) → integer

Scale of the argument (the number of decimal digits in the fractional part) 引数の位取り(小数点以下の10進の桁数)

scale(8.4100)4

sign ( numeric ) → numeric

sign ( double precision ) → double precision

Sign of the argument (-1, 0, or +1) 引数の符号(-1, 0,あるいは +1)

sign(-8.4)-1

sqrt ( numeric ) → numeric

sqrt ( double precision ) → double precision

Square root 平方根

sqrt(2)1.4142135623730951

trim_scale ( numeric ) → numeric

Reduces the value's scale (number of fractional decimal digits) by removing trailing zeroes 後方のゼロを削除することにより値の桁数(小数点以下の10進桁数)を減じる

trim_scale(8.4100)8.41

trunc ( numeric ) → numeric

trunc ( double precision ) → double precision

Truncates to integer (towards zero) 整数へ切り捨て(ゼロに向かって)

trunc(42.8)42

trunc(-42.8)-42

trunc ( v numeric, s integer ) → numeric

Truncates <parameter>v</parameter> to <parameter>s</parameter> decimal places vを小数点以下s桁で切り捨て

trunc(42.4382, 2)42.43

width_bucket ( operand numeric, low numeric, high numeric, count integer ) → integer

width_bucket ( operand double precision, low double precision, high double precision, count integer ) → integer

Returns the number of the bucket in which <parameter>operand</parameter> falls in a histogram having <parameter>count</parameter> equal-width buckets spanning the range <parameter>low</parameter> to <parameter>high</parameter>. Returns <literal>0</literal> or <literal><parameter>count</parameter>+1</literal> for an input outside that range. lowからhighまでの範囲に広がる等幅でバケット数countのヒストグラムにおいて、operandが割り当てられるバケット番号を返す。 範囲外の入力値に対しては0またはcount+1を返す。

width_bucket(5.35, 0.024, 10.06, 5)3

width_bucket ( operand anycompatible, thresholds anycompatiblearray ) → integer

Returns the number of the bucket in which <parameter>operand</parameter> falls given an array listing the lower bounds of the buckets. Returns <literal>0</literal> for an input less than the first lower bound. <parameter>operand</parameter> and the array elements can be of any type having standard comparison operators. The <parameter>thresholds</parameter> array <emphasis>must be sorted</emphasis>, smallest first, or unexpected results will be obtained. バケットの最小値を示す配列が与えられた時に、operandが割り当てられるバケット番号を返す。 最初の最小値よりも小さい入力値に対しては0を返す。 operandと配列要素は標準の比較演算子を持つ型であればどのような型でも構いません。 thresholds配列はソートされていなければならず、小さいものが最初です。 さもなければ予想外の結果となるでしょう。

width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[])2


<xref linkend="functions-math-random-table"/> shows functions for generating random numbers. 表 9.6に乱数を生成する関数を示します。

表9.6 乱数関数

<title>Random Functions</title>

Function 関数

Description 説明

Example(s)

random ( ) → double precision

Returns a random value in the range 0.0 &lt;= x &lt; 1.0 0.0 <= x < 1.0の範囲の乱数値を返す

random()0.897124072839091

random_normal ( [ mean double precision [, stddev double precision ]] ) → double precision

Returns a random value from the normal distribution with the given parameters; <parameter>mean</parameter> defaults to 0.0 and <parameter>stddev</parameter> defaults to 1.0 指定されたパラメータを使用した標準偏差での乱数値を返します。 meanのデフォルト値は0.0、stddevのデフォルト値は1.0です。

random_normal(0.0, 1.0)0.051285419

setseed ( double precision ) → void

Sets the seed for subsequent <literal>random()</literal> and <literal>random_normal()</literal> calls; argument must be between -1.0 and 1.0, inclusive 今後のrandom()random_normal()呼び出しで使用されるシード(種)を設定します。引数は-1.0から1.0までの境界を含む値でなければなりません。

setseed(0.12345)


The <function>random()</function> function uses a deterministic pseudo-random number generator. It is fast but not suitable for cryptographic applications; see the <xref linkend="pgcrypto"/> module for a more secure alternative. If <function>setseed()</function> is called, the series of results of subsequent <function>random()</function> calls in the current session can be repeated by re-issuing <function>setseed()</function> with the same argument. Without any prior <function>setseed()</function> call in the same session, the first <function>random()</function> call obtains a seed from a platform-dependent source of random bits. These remarks hold equally for <function>random_normal()</function>. random()関数は決定論的疑似乱数発生器を使用しています。 高速ですが、暗号用途には適していません。より安全な代替物としてpgcryptoモジュールを参照してください。 setseed()が呼び出されると、現在のセッション内での以後の一連のrandom()の呼び出し結果はsetseed()を同じ引数で再実行することによって再現可能となります。 同じセッション内でsetseed()をそれ以前に呼び出していない場合は、最初のrandom()の呼び出しによってプラットフォーム依存の乱数ビットのソースからシードを入手します。 これらの注意点はrandom_normal()にも適用されます。

<xref linkend="functions-math-trig-table"/> shows the available trigonometric functions. Each of these functions comes in two variants, one that measures angles in radians and one that measures angles in degrees. 使用可能な三角関数を表 9.7に示します。 それぞれの三角関数には、角度の単位をラディアンにするものと度にするものの2種類があります。

表9.7 三角関数

<title>Trigonometric Functions</title>

Function 関数

Description 説明

Example(s)

acos ( double precision ) → double precision

Inverse cosine, result in radians 逆余弦関数、結果はラディアン

acos(1)0

acosd ( double precision ) → double precision

Inverse cosine, result in degrees 逆余弦関数、結果は度

acosd(0.5)60

asin ( double precision ) → double precision

Inverse sine, result in radians 逆正弦関数、結果はラディアン

asin(1)1.5707963267948966

asind ( double precision ) → double precision

Inverse sine, result in degrees 逆正弦関数、結果は度

asind(0.5)30

atan ( double precision ) → double precision

Inverse tangent, result in radians 逆正接関数、結果はラディアン

atan(1)0.7853981633974483

atand ( double precision ) → double precision

Inverse tangent, result in degrees 逆正接関数、結果は度

atand(1)45

atan2 ( y double precision, x double precision ) → double precision

Inverse tangent of <parameter>y</parameter>/<parameter>x</parameter>, result in radians y/xの逆正接関数、結果はラディアン

atan2(1, 0)1.5707963267948966

atan2d ( y double precision, x double precision ) → double precision

Inverse tangent of <parameter>y</parameter>/<parameter>x</parameter>, result in degrees y/xの逆正接関数、結果は度

atan2d(1, 0)90

cos ( double precision ) → double precision

Cosine, argument in radians 余弦関数、引数はラディアン

cos(0)1

cosd ( double precision ) → double precision

Cosine, argument in degrees 余弦関数、引数は度

cosd(60)0.5

cot ( double precision ) → double precision

Cotangent, argument in radians 余接関数、引数はラディアン

cot(0.5)1.830487721712452

cotd ( double precision ) → double precision

Cotangent, argument in degrees 余接関数、引数は度

cotd(45)1

sin ( double precision ) → double precision

Sine, argument in radians 正弦関数、結果はラディアン

sin(1)0.8414709848078965

sind ( double precision ) → double precision

Sine, argument in degrees 正弦関数、結果は度

sind(30)0.5

tan ( double precision ) → double precision

Tangent, argument in radians 正接関数、引数はラディアン

tan(1)1.5574077246549023

tand ( double precision ) → double precision

Tangent, argument in degrees 正接関数、引数は度

tand(45)1


注記

Another way to work with angles measured in degrees is to use the unit transformation functions <literal><function>radians()</function></literal> and <literal><function>degrees()</function></literal> shown earlier. However, using the degree-based trigonometric functions is preferred, as that way avoids round-off error for special cases such as <literal>sind(30)</literal>. 度単位の角度を扱う別の方法は、前に示した単位変換関数radians()degrees()を使うことです。 しかし、角度を使う方法の方が、sind(30)のような特別な場合の丸め誤差を避けられるため、推奨されます。

<xref linkend="functions-math-hyp-table"/> shows the available hyperbolic functions. 表 9.8に利用可能な双曲線関数を示します。

表9.8 双曲線関数

<title>Hyperbolic Functions</title>

Function 関数

Description 説明

Example(s)

sinh ( double precision ) → double precision

Hyperbolic sine 双曲線正弦

sinh(1)1.1752011936438014

cosh ( double precision ) → double precision

Hyperbolic cosine 双曲線余弦

cosh(0)1

tanh ( double precision ) → double precision

Hyperbolic tangent 双曲線正接

tanh(1)0.7615941559557649

asinh ( double precision ) → double precision

Inverse hyperbolic sine 逆双曲線正弦

asinh(1)0.881373587019543

acosh ( double precision ) → double precision

Inverse hyperbolic cosine 逆双曲線余弦

acosh(1)0

atanh ( double precision ) → double precision

Inverse hyperbolic tangent 逆双曲線正接

atanh(0.5)0.5493061443340548