模数、浮点数

发布于 2024-10-09 06:33:10 字数 92 浏览 0 评论 0原文

如何对浮点数使用模?例如,如何查找结果

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍笑 2024-10-16 06:33:10

在某些情况下,可能需要对大数使用模,一种方法是使用十进制(因为 int 和 bigint 可能太小)。

您必须自己实现模运算符 - 一种方法是:

SELECT (x/y-ROUND(x/y,0,1))*y

ROUND(x/y,0,1) 函数返回截断的x/y。例如,

(10.0/3 - ROUND(10.0/3,0,1))*3 = (3.333333-3)*3 = 1 

这在解决大整数的模运算(例如 24840081102364802172700 mod 97 )时非常方便:

SELECT (CAST(24840081102364802172700 AS DECIMAL(23,0))/97 - ROUND(CAST(24840081102364802172700 AS DECIMAL(23,0))/97 ,0,1)) *97 = 10.0

您也可以最后将其完全舍入。

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:

SELECT (x/y-ROUND(x/y,0,1))*y

ROUND(x/y,0,1) function returns truncated x/y. For example,

(10.0/3 - ROUND(10.0/3,0,1))*3 = (3.333333-3)*3 = 1 

This comes handy in solving modulo operations for huge integers such as 24840081102364802172700 mod 97 :

SELECT (CAST(24840081102364802172700 AS DECIMAL(23,0))/97 - ROUND(CAST(24840081102364802172700 AS DECIMAL(23,0))/97 ,0,1)) *97 = 10.0

You can also round it altogether in the end.

画中仙 2024-10-16 06:33:10

对于较小的数字,您必须将其转换为十进制

 select cast(power(cast(101 as float),50) as decimal(38,0)) % 221

,或者

 select power(cast(101 as decimal(38,0)),50) % 221

尽管对于如此大的数字,这会失败,

但是对于较大的数字,无论如何它都没有意义。

  • float 精确到 15 位有效数字。
  • 101 ^ 50 = 1.64463182184388E+100
  • 幅度(浮点近似)大约比模 221 高 82 个数量级 (1E+82)

模数的任何答案都是垃圾

编辑:

小数约为 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

 select cast(power(cast(101 as float),50) as decimal(38,0)) % 221

or

 select power(cast(101 as decimal(38,0)),50) % 221

This fails though with such a large number

But then it makes no sense anyway for larger numbers.

  • float is accurate to 15 signficant figures.
  • 101 ^ 50 = 1.64463182184388E+100
  • the margin of error (float approximation) is about 82 orders of magnitude (1E+82) higher than your modulo 221

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

贪恋 2024-10-16 06:33:10

如果 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文