天天看点

PostgreSQL Oracle兼容性之 - REMAINDER

在postgresql数据库中常用的取余函数为mod,oracle另外还提供了一个取余的函数remainder,它与mod的区别在于,mod取余时用了floor处理,而remainder使用round处理。

算法

1. mod

2. remainder

remainder(n2, n1)

remainder returns the remainder of n2 divided by n1.

this function takes as arguments any numeric datatype or any nonnumeric datatype that can be implicitly converted to a numeric datatype.

oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

the mod function is similar to remainder except that it uses floor in its formula, whereas remainder uses round.

if n1 = 0 or m2 = infinity, then oracle returns

an error if the arguments are of type number

nan if the arguments are binary_float or binary_double.

if n1 != 0, then the remainder is n2 - (n1*n) where n is the integer nearest n2/n1.

if n2 is a floating-point number, and if the remainder is 0, then the sign of the remainder is the sign of n2.

remainders of 0 are unsigned for number values.

了解算法之后,pg就很容易实现remainder的函数了。

验证结果

<a href="http://docs.oracle.com/cd/b19306_01/server.102/b14200/functions133.htm">http://docs.oracle.com/cd/b19306_01/server.102/b14200/functions133.htm</a>

<a href="http://docs.oracle.com/cd/b19306_01/server.102/b14200/functions088.htm">http://docs.oracle.com/cd/b19306_01/server.102/b14200/functions088.htm</a>

<a href="http://info.flagcounter.com/h9v1">count</a>