模数、浮点数
如何对浮点数使用模?例如,如何查找结果
select power(cast(101 as float),50)%221
How to use modulo for float numbers? For example, how to find the result of
select power(cast(101 as float),50)%221
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在某些情况下,可能需要对大数使用模,一种方法是使用十进制(因为 int 和 bigint 可能太小)。
您必须自己实现模运算符 - 一种方法是:
ROUND(x/y,0,1)
函数返回截断的x/y
。例如,这在解决大整数的模运算(例如 24840081102364802172700 mod 97 )时非常方便:
您也可以最后将其完全舍入。
There are situations where one might need to use modulo on large numbers, and one way to do this is using decimal (since int and bigint might be too small).
You have to implement modulo operator yourself - one way is this:
ROUND(x/y,0,1)
function returns truncatedx/y
. For example,This comes handy in solving modulo operations for huge integers such as 24840081102364802172700 mod 97 :
You can also round it altogether in the end.
对于较小的数字,您必须将其转换为十进制
,或者
尽管对于如此大的数字,这会失败,
但是对于较大的数字,无论如何它都没有意义。
模数的任何答案都是垃圾
编辑:
小数约为 10^38
误差 10^39 或 1E+39 处的浮点数,则精确到 1E24 左右(15 位有效数字)。
你的模数是 221 = 2.2E+2
你的误差幅度即 1E+24/2.2E+2 = 4.4E+21
为了100%清楚,你的精度是你的模数的 4,400,000,000,000,000,000,000,000 倍。
它甚至不是近似值:它是垃圾
You'd have to cast to decimal for smaller numbers
or
This fails though with such a large number
But then it makes no sense anyway for larger numbers.
Any answer from the modulo is utter rubbish
Edit:
Decimal goes to around 10^38
Take a float number at 10^39, or 1E+39, then you are accurate to around 1E24 (15 signficant figures).
Your modulo is 221 = 2.2E+2
You margin of error ie 1E+24/2.2E+2 = 4.4E+21
Just to be 100% clear, your accuracy is 4,400,000,000,000,000,000,000,000 times greater than your modulo.
It isn't even approximate: it's rubbish
如果 SQL Server 支持,mod 函数应该处理浮点数。但是您显示的计算结果是,浮点数无法提供足够的精度。当你离开演员阵容时会发生什么?
If SQL Server supports it, the mod function should handle floats. But the computation you show, a float will not provide adequate precision. What happens when you leave off the cast?