如果没有 mod 运算符,我该如何进行 mod 操作?
该脚本语言没有 % 或 Mod()。我确实有一个 Fix() 可以去掉数字的小数部分。我只需要积极的结果,所以不要太强硬。
This scripting language doesn't have a % or Mod(). I do have a Fix() that chops off the decimal part of a number. I only need positive results, so don't get too robust.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
会
做?我假设你至少可以在这里划分。所有赌注都押在负数上。
Will
do? I'm assuming you can at least divide here. All bets are off on negative numbers.
对于后代,BrightScript 现在有一个模运算符,它看起来像这样:
For posterity, BrightScript now has a modulo operator, it looks like this:
a mod n = a - (n * Fix(a/n))
a mod n = a - (n * Fix(a/n))
如果有人稍后到达,这里有一些更实际的算法(有错误......仔细阅读)
https: //eprint.iacr.org/2014/755.pdf
实际上有两种主要的归约公式:Barett 和 Montgomery。 eprint 的论文在不同版本(算法 1-3)中重复了这两个版本,并在算法 4 中给出了“改进”版本。
概述
我现在概述了 4. 算法:
1.) 计算“A*B”并存储“C”中的整个乘积,C 和模数 $p$ 是该算法的输入。
2.) 计算 $p$ 的位长度,例如:函数“Width(p)”返回的正是该值。
3.) 将输入 $C$ 分割为 N 个大小为“Width(p)”的“块”并将每个块存储在 G 中。从 G[0] = lsb(p) 开始,以 G[N-1] = msb 结束(页)。 (论文的描述确实有错误)
4.)启动 while 循环:
设置N=N-1(到达最后一个元素)
预先计算 $b:=2^{Width(p)} \bmod p$
这确实有很多作用。不是我们只需要递归地约简G[0]:
其他三种算法都定义良好,但是这缺少一些信息或者表现得非常错误。但它适用于任何尺寸;)
If someone arrives later, here are some more actual algorithms (with errors...read carefully)
https://eprint.iacr.org/2014/755.pdf
There are actually two main kind of reduction formulae: Barett and Montgomery. The paper from eprint repeat both in different versions (algorithms 1-3) and give an "improved" version in algorithm 4.
Overview
I give now an overview of the 4. algorithm:
1.) Compute "A*B" and Store the whole product in "C" that C and the modulus $p$ is the input for that algorithm.
2.) Compute the bit-length of $p$, say: the function "Width(p)" returns exactly that value.
3.) Split the input $C$ into N "blocks" of size "Width(p)" and store each in G. Start in G[0] = lsb(p) and end in G[N-1] = msb(p). (The description is really faulty of the paper)
4.) Start the while loop:
Set N=N-1 (to reach the last element)
precompute $b:=2^{Width(p)} \bmod p$
That does alot. Not we only need to recursivly reduce G[0]:
The other three algorithms are well defined, but this lacks some information or present it really wrong. But it works for any size ;)
它是什么语言?
基本算法可能是:
What language is it?
A basic algorithm might be:
这可能对您的性能不起作用,但是:
This may not work for you performance-wise, but:
在 JavaScript 中:
In javascript: