数学函数和操作符#

OushuDB 为很多类型提供了数学操作符。对于那些没有常用数学传统的可能排列(比如日期/时间类型),我们在随后的章节里描述了实际的行为。

下表显示了可用的数学操作符。

表.数学操作符

操作符

描述

例子

结果

+

2 + 3

5

-

2 - 3

-1

*

2 * 3

6

/

除(整数除法将截断结果)

4 / 2

2

%

模(求余)

5 % 4

1

^

指数运算

2.0 ^ 3.0

8.0000000000000000

|/

平方根

|/ 25.0

5

||/

立方根

||/ 27.0

3

!

阶乘

5 !

120

!!

阶乘(前缀运算符)

!! 5

120

@

绝对值

@ -5.0

5.0

&

按位 AND

91 & 15

11

|

按位 OR

32 | 3

35

#

按位 XOR

17 # 5

20

~

按位 NOT

~1

-2

<<

按位左移

1 << 4

16

>>

按位右移

8 >> 2

2

按位操作符只能用于整数类型,而其他的操作符可用于所有数值类型。按位操作符还可以用于位串类型bit 和bit varying。

下表展示了可用的数学函数。在表中,dp 代表double precision。这些函数中很多有多种形式,区别是参数不同。除非特别声明,任何给定形式的函数返回和它参数同样类型的结果。处理double precision 数据的函数大多数是在宿主系统的C 库的基础上实现的;因此,精度和数值范围方面的行为可能根据宿主系统而变化。

表.数学函数

函数

返回类型

描述

例子

结果

abs (x)

与x相同

绝对值

abs(-17.4)

17.4

cbrt (dp)

dp

立方根

cbrt (27.0)

3

ceil (dp or numeric)

与输入相同

不小于参数的最小整数

ceil (-42.8)

-42

ceiling (dp or numeric)

与输入相同

不小于参数的最小整数(ceil的别名)

ceiling (-95.3)

-95

degrees (dp)

dp

把弧度转成角度

degrees (0.5)

28.6478897565412

exp (dp or numeric)

与输入相同

指数

exp (1.0)

2.7182818284590452

floor (dp or numeric)

与输入相同

不大于参数的最大整数

floor (-42.8)

-43

ln (dp or numeric)

与输入相同

自然对数

ln (2.0)

0.6931471805599453

log (dp or numeric)

与输入相同

以10为底的对数

log (100.0)

2.0000000000000000

log (b numeric, x numeric)

numeric

以b为底的对数

log (2.0, 64.0)

6.0000000000000000

mod (y,x)

与参数类型相同

y/x的余数

mod (9,4)

1

pi ()

dp

π 常量

pi ()

3.14159265358979

power (a dp, b dp)

dp

a的b次幂

power (9.0, 3.0)

729

power (a numeric, b numeric)

numeric

a的b次幂

power (9.0, 3.0)

729.0000000000000000

radians (dp)

dp

把角度转化为弧度

radians (45.0)

0.785398163397448

random ()

dp

0.0到1.0之间的随机数

random ()

round (dp or numeric)

与输入相同

近似为最接近的整数

round (42.4)

42

round (v numeric, s int)

与输入相同

近似到小数点后s位

round (42.4382, 2)

42.44

setseed (dp)

int

为接下来的random()调用(其值在0-1.0)设置种子

setseed (0.54823)

0

sign (dp or numeric)

与输入相同

参数的符号(-1, 0, +1)

sign (-8.4)

-1

sqrt (dp or numeric)

与输入相同

平方根

sqrt (2.0)

1.414213562373095

trunc (dp or numeric)

与输入相同

截断向0接近

trunc (42.8)

42

trunc (v numeric, s int)

与输入相同

截断到小数点后s位

trunc (42.4382, 2)

42.43

width_bucket (op numeric, b1 numeric, b2 numeric, count int)

int

返回一个桶,这个桶是在一个有count个桶,上界为b1下界为b2的等深柱状图中operand将会被赋予的那个桶

width_bucket (5.35, 0.024, 10.06, 5)

3

最后,下表展示了可用的三角函数,所有的三角函数的参数和返回值类型都是double precision。

表.三角函数

这里x,y取值应该在-1和1之间,否则会报错。

函数

描述

acos (x)

反余弦

asin (x)

反正弦

atan (x)

反正切

atan2 (x,y)

x/y的反正切

cos (x)

余弦

cot (x)

余切

sin (x)

正弦

tan (x)

正切