9.3. 数学函数和操作符 |
您所在的位置:网站首页 › 正切相加 › 9.3. 数学函数和操作符 |
9.3. 数学函数和操作符 PostgreSQL为很多类型提供了数学操作符。对于那些没有标准数学表达的类型(如日期/时间类型),我们将在后续小节中描述实际的行为。 表 9.4 显示了可用于标准数字类型的数学操作符。. 除非另有说明, 显示为可接受 numeric_type 的操作符对所有的 smallint、integer、bigint、numeric、real 和 double precision类型都可用。 显示为可接受 integral_type 的操作符对 smallint、integer 和 bigint类型是可用的。 除了特别说明之处,操作符的每种形式都返回与其参数相同的数据类型。 涉及多个参数数据类型的调用, 例如 integer + numeric,可通过使用这些列表中稍后出现的类型来解析。 表 9.4. 数学操作符 操作符 描述 例子 numeric_type + numeric_type → numeric_type 加 2 + 3 → 5 + numeric_type → numeric_type 一元加(无操作) + 3.5 → 3.5 numeric_type - numeric_type → numeric_type 减 2 - 3 → -1 - numeric_type → numeric_type 否定 - (-4) → 4 numeric_type * numeric_type → numeric_type 乘 2 * 3 → 6 numeric_type / numeric_type → numeric_type 除(对于整型,除法将结果截断为零) 5.0 / 2 → 2.5000000000000000 5 / 2 → 2 (-5) / 2 → -2 numeric_type % numeric_type → numeric_type 模(取余); 适用于 smallint,integer,bigint 和 numeric 5 % 4 → 1 numeric ^ numeric → numeric double precision ^ double precision → double precision 指数 (不像典型的数学实践, 多次使用 ^ 将会从左到有关联) 2 ^ 3 → 8 2 ^ 3 ^ 3 → 512 |/ double precision → double precision 平方根 |/ 25.0 → 5 ||/ double precision → double precision 立方根 ||/ 64.0 → 4 bigint ! → numeric 阶乘(已弃用, 使用 factorial() 代替) 5 ! → 120 !! bigint → numeric 阶乘作为前缀操作符(已弃用, 使用 factorial() 代替) !! 5 → 120 @ numeric_type → numeric_type 绝对值 @ -5.0 → 5 integral_type & integral_type → integral_type 按位与(AND) 91 & 15 → 11 integral_type | integral_type → integral_type 按位或(OR) 32 | 3 → 35 integral_type # integral_type → integral_type 按位异或(exclusive OR) 17 # 5 → 20 ~ integral_type → integral_type 按位求反(NOT) ~1 → -2 integral_type > 2 → 2 表 9.5 显示了可用的数学函数。 许多这样的函数以多种具有不同的参数类型的形式提供。 除非注明,任何给定形式的函数都返回与其参数相同的数据类型;跨类型情况的解决方法与上述对操作符的解释相同。 使用double precision数据的函数大多是在主机系统的C库上实现的; 因此,边界情况下的准确性和行为会因主机系统的区别而不同。 表 9.5. 数学函数 函数 描述 例子 abs ( numeric_type ) → numeric_type 绝对值 abs(-17.4) → 17.4 cbrt ( double precision ) → double precision 立方根 cbrt(64.0) → 4 ceil ( numeric ) → numeric ceil ( double precision ) → double precision 大于或等于参数的最接近的整数 ceil(42.2) → 43 ceil(-42.8) → -42 ceiling ( numeric ) → numeric ceiling ( double precision ) → double precision 大于或等于参数的最接近的整数 (与 ceil 相同) ceiling(95.3) → 96 degrees ( double precision ) → double precision 将弧度转换为角度 degrees(0.5) → 28.64788975654116 div ( y numeric, x numeric ) → numeric y/x 的整数商(截断为零位) div(9,4) → 2 exp ( numeric ) → numeric exp ( double precision ) → double precision 指数 (e 的给定次方) exp(1.0) → 2.7182818284590452 factorial ( bigint ) → numeric 阶乘 factorial(5) → 120 floor ( numeric ) → numeric floor ( double precision ) → double precision 小于或等于参数的最接近整数 floor(42.8) → 42 floor(-42.8) → -43 gcd ( numeric_type, numeric_type ) → numeric_type 最大公约数 (能将两个输入数整除而无余数的最大正数); 如果两个输入为零则返回 0 ; 适用于 integer, bigint,和 numeric gcd(1071, 462) → 21 lcm ( numeric_type, numeric_type ) → numeric_type 最小公倍数(两个输入的整数倍的最小的严格正数);如果任意一个输入值为零则返回0;适用于integer,bigint,和 numeric lcm(1071, 462) → 23562 ln ( numeric ) → numeric ln ( double precision ) → double precision 自然对数 ln(2.0) → 0.6931471805599453 log ( numeric ) → numeric log ( double precision ) → double precision 以10为底的对数 log(100) → 2 log10 ( numeric ) → numeric log10 ( double precision ) → double precision 以10为底的对数 (与 log 相同) log10(1000) → 3 log ( b numeric, x numeric ) → numeric 以 b 为底的 x的对数 log(2.0, 64.0) → 6.0000000000 min_scale ( numeric ) → integer 精确表示所提供值所需的最小刻度(小数位数) min_scale(8.4100) → 2 mod ( y numeric_type, x numeric_type ) → numeric_type y/x的余数; 适用于smallint、integer、bigint、和 numeric mod(9,4) → 1 pi ( ) → double precision π的近似值 pi() → 3.141592653589793 power ( a numeric, b numeric ) → numeric power ( a double precision, b double precision ) → double precision a的b次幂 power(9, 3) → 729 radians ( double precision ) → double precision 将角度转换为弧度 radians(45.0) → 0.7853981633974483 round ( numeric ) → numeric round ( double precision ) → double precision 四舍五入到最近的整数 round(42.4) → 42 round ( v numeric, s integer ) → numeric 把 v 四舍五入到 s 位小数 round(42.4382, 2) → 42.44 scale ( numeric ) → integer 参数的刻度(小数点后的位数) scale(8.4100) → 4 sign ( numeric ) → numeric sign ( double precision ) → double precision 参数的符号 (-1, 0, 或 +1) sign(-8.4) → -1 sqrt ( numeric ) → numeric sqrt ( double precision ) → double precision 平方根 sqrt(2) → 1.4142135623730951 trim_scale ( numeric ) → numeric 通过删除尾数部分的零来降低值的刻度(小数位数) trim_scale(8.4100) → 8.41 trunc ( numeric ) → numeric trunc ( double precision ) → double precision 截断整数 (向零靠近) trunc(42.8) → 42 trunc(-42.8) → -42 trunc ( v numeric, s integer ) → numeric 截断 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 返回包含count等宽柱的柱状图中operand所在的柱的编号,范围从low到high。 超出该范围的输入则返回0或计数+1。 width_bucket(5.35, 0.024, 10.06, 5) → 3 width_bucket ( operand anyelement, thresholds anyarray ) → integer 返回一个柱号,这个柱是在给定数组中operand将被分配的柱。 对于一个低于第一个下界的输入返回0。 operand和数组元素可以是具有标准比较操作符的任何类型。 thresholds数组必须被排好序,最小的排在最前面,否则将会得到意想不到的结果。 width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]) → 2 表 9.6展示了用于产生随机数的函数。 表 9.6. 随机函数 函数 描述 例子 random ( ) → double precision 返回一个范围 0.0 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |