MySQL MOD() is broken: Is this the best alternative?

若如初见. 提交于 2019-12-06 10:41:17

MySQL isn't giving you a bogus result, it's simply using a different implementation of modulus than you are expecting. Unfortunately the term modulus seems to have been defined somewhat ambiguously, and it's implementation varies from language to language. From what I can tell in the Wikipedia on Modulo, MySQL's implementation is using truncated division:

r = a - n * trunc(a / n)

Where you are expecting the implementation to use floored division:

r = a - n * floor(a / n)

Since this how you implemented your first workaround, I'd say it's probably the best alternative to the Mod operator.

From what I've seen (and this is a very quick unscientific analysis!), it seems like more imperative programming languages implement truncated division and more functional mathematical languages seem to use floored division.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!