如何在没有“%”运算符的情况下计算两个数字的 rest/modulo?
How do I calculate the rest/modulo of two numbers without the "%" operator?
例如使用以下输入:
int num = -100
int divisor = 10
=> -100 mod 10 = 0 (Edge-case: negative numbers as input)
int num = 300
int divisor = -7
=>300 mod 7 = 6
我以前用过这个方法,但是用负数,它不起作用:
int method(int i, int div){
return (num - divisor * (num / divisor));
}
预期结果:
-1234, 512 ==> <302>
实际结果:
-1234, 512 ==> <210>
以下测试证明您的初始实施提供与原始 %
运算符相同的结果:
static int mod(int n, int m) {
return n - m * (n / m);
}
// test
int[][] data = {
{100, 10}, {-100, 10}, {100, -10}, {-100, -10},
{ 5, 3}, {- 5, 3}, { 5, - 3}, {- 5, - 3}
};
Arrays.stream(data)
.forEach(d -> System.out.printf("%s: %d %% %d = %d mod=%d%n",
d[0]%d[1] == mod(d[0], d[1]) ? "OK" : "BAD",
d[0], d[1], d[0]%d[1], mod(d[0], d[1])));
输出:
OK: 100 % 10 = 0 mod=0
OK: -100 % 10 = 0 mod=0
OK: 100 % -10 = 0 mod=0
OK: -100 % -10 = 0 mod=0
OK: 5 % 3 = 2 mod=2
OK: -5 % 3 = -2 mod=-2
OK: 5 % -3 = 2 mod=2
OK: -5 % -3 = -2 mod=-2
您的代码在有模和无模的情况下都运行良好。
对于负数和正数,它们都给出了相同的结果。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Dividend : ");
int i = scan.nextInt();
System.out.println("\nEnter the Divisor : ");
int div = scan.nextInt();
System.out.println("Without modulo :Dividend: " + i + " , Divisor: " + div + ", Remainder: " +(i - div * (i / div)));
System.out.println("With modulo :Dividend: " + i + " , Divisor: " + div + ", Remainder: " +(i % div));
System.out.println("Using abstract :Dividend: " + i + " , Divisor: " + div + ", Remainder: " +Math.abs(i - div * (i / div)));
}
}
输出:
Enter the Dividend :
300
Enter the Divisor :
-7
Without modulo :Dividend: 300 , Divisor: -7, Remainder: 6
With module :Dividend: 300 , Divisor: -7, Remainder: 6
Using abstract :Dividend: 300, Divisor: -7, Remainder: 6
Enter the Dividend :
-1234
Enter the Divisor :
512
Without modulo :Dividend: -1234 , Divisor: 512, Remainder: -210
With module :Dividend: -1234 , Divisor: 512, Remainder: -210
Using abstract :Dividend: -1234 , Divisor: 512, Remainder: 210
这是你的方法。
static int method(int num, int div){
return num - div * (num / div);
}
它工作正常,因为它 returns 余数,不管使用的符号如何,就像
%
。但是,如果您想要更多 positive mod
答案,则需要执行以下操作:
static int method(int num, int div){
int mod = num - div * (num / div);
return (mod < 0) ? mod + div : mod;
}
来自 Java Virtual Machine Specification for irem.
Both value1 and value2 must be of type int. The values are popped
from the operand stack. The int result is value1 - (value1 / value2)
* value2. The result is pushed onto the operand stack.
例如使用以下输入:
int num = -100
int divisor = 10
=> -100 mod 10 = 0 (Edge-case: negative numbers as input)
int num = 300
int divisor = -7
=>300 mod 7 = 6
我以前用过这个方法,但是用负数,它不起作用:
int method(int i, int div){
return (num - divisor * (num / divisor));
}
预期结果:
-1234, 512 ==> <302>
实际结果:
-1234, 512 ==> <210>
以下测试证明您的初始实施提供与原始 %
运算符相同的结果:
static int mod(int n, int m) {
return n - m * (n / m);
}
// test
int[][] data = {
{100, 10}, {-100, 10}, {100, -10}, {-100, -10},
{ 5, 3}, {- 5, 3}, { 5, - 3}, {- 5, - 3}
};
Arrays.stream(data)
.forEach(d -> System.out.printf("%s: %d %% %d = %d mod=%d%n",
d[0]%d[1] == mod(d[0], d[1]) ? "OK" : "BAD",
d[0], d[1], d[0]%d[1], mod(d[0], d[1])));
输出:
OK: 100 % 10 = 0 mod=0
OK: -100 % 10 = 0 mod=0
OK: 100 % -10 = 0 mod=0
OK: -100 % -10 = 0 mod=0
OK: 5 % 3 = 2 mod=2
OK: -5 % 3 = -2 mod=-2
OK: 5 % -3 = 2 mod=2
OK: -5 % -3 = -2 mod=-2
您的代码在有模和无模的情况下都运行良好。
对于负数和正数,它们都给出了相同的结果。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Dividend : ");
int i = scan.nextInt();
System.out.println("\nEnter the Divisor : ");
int div = scan.nextInt();
System.out.println("Without modulo :Dividend: " + i + " , Divisor: " + div + ", Remainder: " +(i - div * (i / div)));
System.out.println("With modulo :Dividend: " + i + " , Divisor: " + div + ", Remainder: " +(i % div));
System.out.println("Using abstract :Dividend: " + i + " , Divisor: " + div + ", Remainder: " +Math.abs(i - div * (i / div)));
}
}
输出:
Enter the Dividend :
300
Enter the Divisor :
-7
Without modulo :Dividend: 300 , Divisor: -7, Remainder: 6
With module :Dividend: 300 , Divisor: -7, Remainder: 6
Using abstract :Dividend: 300, Divisor: -7, Remainder: 6
Enter the Dividend :
-1234
Enter the Divisor :
512
Without modulo :Dividend: -1234 , Divisor: 512, Remainder: -210
With module :Dividend: -1234 , Divisor: 512, Remainder: -210
Using abstract :Dividend: -1234 , Divisor: 512, Remainder: 210
这是你的方法。
static int method(int num, int div){
return num - div * (num / div);
}
它工作正常,因为它 returns 余数,不管使用的符号如何,就像
%
。但是,如果您想要更多 positive mod
答案,则需要执行以下操作:
static int method(int num, int div){
int mod = num - div * (num / div);
return (mod < 0) ? mod + div : mod;
}
来自 Java Virtual Machine Specification for irem.
Both value1 and value2 must be of type int. The values are popped from the operand stack. The int result is value1 - (value1 / value2) * value2. The result is pushed onto the operand stack.