gpt4 book ai didi

Java - 是否有欧几里德或地板模的方法

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:43 24 4
gpt4 key购买 nike

Java 模运算符 %基于截断的除法(参见 Wikipedia: Modulo operation )。

  • 5%3生产 2 (注意 5/3 产生 1 )
  • 5%(-3)生产 2 (注意 5/(-3) 产生 -1 )
  • (-5)%3生产 -2 (注意 (-5)/3 产生 -1 )
  • (-5)%(-3)生产 -2 (注意 (-5)/(-3) 产生 1 )

在计算科学中,给定两个整数 an , n > 0,有时获取唯一整数 r 很有用在 [a,n[ 内这与 a 一致模 n .

问题

在 Java 中是否有一个有效的泛型运算符/方法来遵守这种模规范?

这是为了避免在每个需要它的项目中重写它...

杂项

我在stackoverflow上发现了很多关于这个问题的问题,其中大部分混淆了不同的取模实现。如果你只是对负数的模运算结果感到困扰,下面是一些基于Java %的实现。可能有用的运算符

常见黑客

由于我们几乎不使用负除数,因此此实现在 n > 0 时返回欧几里得或底模。 .

static int mod(int a, int n){    
return a<0 ? (a%n + n)%n : a%n;
}
  • mod( 5, 3)生产 2
  • mod(-5, 3)生产 1

欧氏模

static int euclideanModulo(int a, int n){
return n<0 ? euclideanModulo(a, -n) : mod(a, n);
}
  • euclideanModulo( 5, 3)生产 2
  • euclideanModulo(-5, 3)生产 1
  • euclideanModulo( 5,-3)生产 2
  • euclideanModulo(-5,-3)生产 1

地板模数

static int flooredModulo(int a, int n){
return n<0 ? -flooredModulo(-a, -n) : mod(a, n);
}
  • flooredModulo( 5, 3)生产 2
  • flooredModulo(-5, 3)生产 1
  • flooredModulo( 5,-3)生产 -1
  • flooredModulo(-5,-3)生产 -2

最佳答案

+----+----+-----------+---------+-----------+-----------+---------+-----------+| x mod y |           quotient 'q'          |          remainder 'r'          || x  | y  | truncated | floored | Euclidean | truncated | floored | Euclidean |+----+----+-----------+---------+-----------+-----------+---------+-----------+|  5 |  3 |         1 |       1 |         1 |         2 |       2 |         2 || -5 |  3 |        -1 |      -2 |        -2 |        -2 |       1 |         1 ||  5 | -3 |        -1 |      -2 |        -1 |         2 |      -1 |         2 || -5 | -3 |         1 |       1 |         2 |        -2 |      -2 |         1 |+----+----+-----------+---------+-----------+-----------+---------+-----------+

Any of them satisfies at least x = yq + r.

Truncated division and modulo

static int truncatedDiv(int x, int y) {    
return x / y;
}

static int truncatedMod(int x, int y) {
return x % y;
}

底除法和取模

从 Java 8 开始,您可以在 java.lang.Math 中使用方法。参见 floorDivfloorMod .

static int floorDiv(int x, int y) {    
return Math.floorDiv(x, y);
}

static int floorMod(int x, int y) {
return Math.floorMod(x, y);
}

欧氏除法和模

a) 基于截断划分

import static java.lang.Math.*;

static int euclideanDiv(int x, int y) {
int r = x / y;
// if the divident is negative and modulo not zero, round down for positive divisor, otherwise round up
if (x < 0 && r * y != x) {
r -= signum(y);
}
return r;
}

static int euclideanMod(int x, int y) {
int r = x - euclideanDiv(x, y) * y;
return r;
}

b) 基于楼层划分

import static java.lang.Math.*;

static int euclideanDiv(int x, int y) {
int r = floorDiv(x, y);
// if the divisor is negative and modulo not zero, round up
if (y < 0 && r * y != x) {
r++;
}
return r;
}

static int euclideanMod(int x, int y) {
int r = x - euclideanDiv(x, y) * y;
return r;
}

c) 基于绝对模

import static java.lang.Math.*;

static int euclideanMod(int x, int y) {
int r = abs(x) % abs(y);
// apply the sign of divident and make sure the remainder is positive number
r *= signum(x);
r = (r + abs(y)) % abs(y);
return r;
}

关于Java - 是否有欧几里德或地板模的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14769943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com