Java Guava | mod(long x, long m) of LongMath Class with Examples
The mod(long x, long m) method of Guava’s LongMath Class accepts two parameters x and m, and used to calculate the value of x modulus under m.
Syntax:
public static long mod(long x, long m)
Parameters: This method accepts two parameters x and m which are of long type to calculate x modulo m.
Return Value: The method returns x mod m that will be a non-negative value less than m.
Exception: The method mod(long x, long m) throws ArithmeticException if m <= 0.
Below examples illustrate the mod(long x, long m) method:
Example 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
Example 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
Recommended Posts:
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- Java Guava | pow(long b, int k) of LongMath Class with Examples
- Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples
- Java Guava | log2(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | LongMath.checkedPow(long b, int k) method with Examples
- Java Guava | LongMath log10(long x, RoundingMode mode) method with Examples
- Java Guava | binomial(int n, int k) of LongMath Class with Examples
- LongMath Class | Guava | Java
- Java Guava | LongMath.checkedMultiply(int a, int b) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



