【问题标题】:remainder function in OracleOracle中的余数函数
【发布时间】:2018-01-09 18:57:16
【问题描述】:

无法在 oracle 中获取剩余()函数。下面是两个带有输出的示例。请通过这些示例让我知道其背后的逻辑,以便我更好地理解。

select remainder(17,3) from dual; // output -1
select remainder(15,6) from dual; // output 3

特别是,为什么 remainder(15,6) 不返回 -3,因为 round(15 / 6) = 3?

【问题讨论】:

  • RTFM - 请阅读手册页
  • 我认为您可能会将余数与MOD 混淆。参考:stackoverflow.com/questions/24449229/…
  • 即使从上面的链接中读取内容,逻辑也与我上面的示例不同步。所以,任何知道并愿意用上面的例子解释的人请发帖。
  • 好问题,虽然措辞有点模糊。 FWIW 不仅仅是阅读文档来了解这里发生了什么,因为文档误导性地暗示 remainder 使用 round,而实际上它对如何进行舍入有自己的想法。

标签: oracle


【解决方案1】:

REMAINDER 是一个比较奇怪的 SQL 函数,我自己没用过。

17 / 3 = 5.66666..
which rounds to 6
17 / 3 = 6 remainder -1

15 / 6 = 2.5
which is rounded to 2 (note: this is NOT using Oracle's default ROUND algorithm!)
15 / 6 = 2 remainder 3

Understanding behavior of remainder() function in Oracle

编辑

根据 Jonathan 的评论,似乎正在使用 round 的 binary_float/binary_double 逻辑,四舍五入到最接近的偶数值。

select  9 / 6, remainder(9,6)
      ,15 / 6, remainder(15,6)
      ,21 / 6, remainder(21,6)
      ,27 / 6, remainder(27,6)
from dual;

1.5 -3
2.5  3
3.5 -3
4.5  3

【讨论】:

  • ROUND() 页面显示:对于 NUMBER 值,值 n 从 0 四舍五入(例如,当 x.5 为正时为 x+1,当为 x-1 x.5 为负数)。对于 BINARY_FLOAT 和 BINARY_DOUBLE 值,函数会舍入到最接近的偶数值。 也许 (15 / 6) 的中间结果被视为 BINARY_DOUBLE 而不是 NUMBER?它至少可以解释观察到的行为。
  • @JonathanLeffler 我认为您正在做一些事情。 remainder(21,6) = -3remainder(27,6) = 3
猜你喜欢
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多