Guava LongMath类的mod(long x,long m)方法接受两个参数x和m,并用于计算m下的x模量值。
用法:
public static long mod(long x, long m)
参数:此方法接受两个参数x和m,它们是长类型的,用于计算x模m。
返回值:该方法返回x mod m,该值将是小于m的非负值。
异常:如果m <= 0,则方法mod(long x,long m)引发ArithmeticException。
以下示例说明了mod(long x,long m)方法:
范例1:
// Java code to show implementation of
// mod(long x, long m) method of Guava's
// LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
long x1 = -77;
long m1 = 4;
long ans1 = LongMath.mod(x1, m1);
// Using mod(long x, long m)
// method of Guava's LongMath class
System.out.println(x1 + " mod "
+ m1 + " is:" + ans1);
long x2 = 22;
long m2 = 6;
long ans2 = LongMath.mod(x2, m2);
// Using mod(long x, long m)
// method of Guava's LongMath class
System.out.println(x2 + " mod "
+ m2 + " is:" + ans2);
}
}
输出:
-77 mod 4 is:3 22 mod 6 is:4
范例2:
// Java code to show implementation of
// mod(long x, long m) method of Guava's
// LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
static long findMod(long x, long m)
{
try {
// Using mod(long x, long m)
// method of Guava's LongMath class
// This should throw "ArithmeticException"
// as m <= 0
long ans = LongMath.mod(x, m);
// Return the answer
return ans;
}
catch (Exception e) {
System.out.println(e);
return -1;
}
}
// Driver code
public static void main(String args[])
{
long x = 11;
long m = -5;
try {
// Function calling
findMod(x, m);
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
java.lang.ArithmeticException:Modulus must be positive
相关用法
- Java Guava LongMath gcd(long a, long b)用法及代码示例
- Java Guava LongMath mean(long x, long y)用法及代码示例
- Java Guava LongMath pow(long b, int k)用法及代码示例
- Java Guava LongMath isPowerOfTwo(long x)用法及代码示例
- Java Guava LongMath sqrt(long x, RoundingMode mode)用法及代码示例
- Java LongMath.divide(long, long, RoundingMode)用法及代码示例
- Java LongMath.checkedAdd(long a, long b)用法及代码示例
- Java LongMath.checkedPow(long b, int k)用法及代码示例
- Java LongMath log10(long x, RoundingMode mode)用法及代码示例
- Java Guava LongMath binomial(int n, int k)用法及代码示例
- Java LongMath.checkedMultiply(int a, int b)用法及代码示例
- Java Longs.checkedSubtract(long a, long b)用法及代码示例
- Java Longs.indexOf(long[] array, long[] target)用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | mod(long x, long m) of LongMath Class with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。