如果没有 mod 运算符,我该如何进行 mod 操作?

发布于 2024-08-31 08:15:25 字数 68 浏览 2 评论 0原文

该脚本语言没有 % 或 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 技术交流群。

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

发布评论

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

评论(7

々眼睛长脚气 2024-09-07 08:15:25

// mod = a % b

c = Fix(a / b)
mod = a - b * c

做?我假设你至少可以在这里划分。所有赌注都押在负数上。

Will

// mod = a % b

c = Fix(a / b)
mod = a - b * c

do? I'm assuming you can at least divide here. All bets are off on negative numbers.

谈情不如逗狗 2024-09-07 08:15:25

对于后代,BrightScript 现在有一个模运算符,它看起来像这样:

c = a mod b

For posterity, BrightScript now has a modulo operator, it looks like this:

c = a mod b
绿光 2024-09-07 08:15:25

a mod n = a - (n * Fix(a/n))

a mod n = a - (n * Fix(a/n))

七堇年 2024-09-07 08:15:25

如果有人稍后到达,这里有一些更实际的算法(有错误......仔细阅读)

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$

while N>0 do:
    T = G[N]
    for(i=0; i<Width(p); i++) do: //Note: that counter doesn't matter, it limits the loop)
        T = T << 1 //leftshift  by 1 bit
        while is_set( bit( T, Width(p) ) ) do // (N+1)-th bit of T is 1
            unset( bit( T, Width(p) ) ) // unset the (N+1)-th bit of T (==0)
            T += b
        endwhile
    endfor
    G[N-1] += T
    while is_set( bit( G[N-1], Width(p) ) ) do
        unset( bit( G[N-1], Width(p) ) ) 
        G[N-1] += b
    endwhile
    N -= 1
endwhile

这确实有很多作用。不是我们只需要递归地约简G[0]:

while G[0] > p do
    G[0] -= p
endwhile
return G[0]// = C mod p

其他三种算法都定义良好,但是这缺少一些信息或者表现得非常错误。但它适用于任何尺寸;)

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$

while N>0 do:
    T = G[N]
    for(i=0; i<Width(p); i++) do: //Note: that counter doesn't matter, it limits the loop)
        T = T << 1 //leftshift  by 1 bit
        while is_set( bit( T, Width(p) ) ) do // (N+1)-th bit of T is 1
            unset( bit( T, Width(p) ) ) // unset the (N+1)-th bit of T (==0)
            T += b
        endwhile
    endfor
    G[N-1] += T
    while is_set( bit( G[N-1], Width(p) ) ) do
        unset( bit( G[N-1], Width(p) ) ) 
        G[N-1] += b
    endwhile
    N -= 1
endwhile

That does alot. Not we only need to recursivly reduce G[0]:

while G[0] > p do
    G[0] -= p
endwhile
return G[0]// = C mod p

The other three algorithms are well defined, but this lacks some information or present it really wrong. But it works for any size ;)

节枝 2024-09-07 08:15:25

它是什么语言?

基本算法可能是:

hold the modulo in a variable (modulo);
hold the target number in a variable (target);
initialize modulus variable;

while (target > 0) {
  if (target > modulo) {
    target -= modulo;
  }
  else if(target < modulo) {
    modulus = target;
    break;
  }
}

What language is it?

A basic algorithm might be:

hold the modulo in a variable (modulo);
hold the target number in a variable (target);
initialize modulus variable;

while (target > 0) {
  if (target > modulo) {
    target -= modulo;
  }
  else if(target < modulo) {
    modulus = target;
    break;
  }
}
诠释孤独 2024-09-07 08:15:25

这可能对您的性能不起作用,但是:

while (num >= mod_limit)
    num = num - mod_limit

This may not work for you performance-wise, but:

while (num >= mod_limit)
    num = num - mod_limit
葬シ愛 2024-09-07 08:15:25

在 JavaScript 中:

function modulo(num1, num2) {    
  if (num2 === 0 || isNaN(num1) || isNaN(num2)) {
    return NaN;
  }

  if (num1 === 0) {
    return 0;
  }

  var remainderIsPositive = num1 >= 0;

  num1 = Math.abs(num1);
  num2 = Math.abs(num2);

  while (num1 >= num2) {
    num1 -= num2
  }

  return remainderIsPositive ? num1 : 0 - num1;
}

In javascript:

function modulo(num1, num2) {    
  if (num2 === 0 || isNaN(num1) || isNaN(num2)) {
    return NaN;
  }

  if (num1 === 0) {
    return 0;
  }

  var remainderIsPositive = num1 >= 0;

  num1 = Math.abs(num1);
  num2 = Math.abs(num2);

  while (num1 >= num2) {
    num1 -= num2
  }

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