Here you can find the source of mod(long x, long y)
public static final long mod(long x, long y)
//package com.java2s; //License from project: Mozilla Public License public class Main { public static final long mod(long x, long y) { return (x - y * floorDivide(x, y)); }//from w w w . java 2 s .c om public static final int mod(int x, int y) { return (x - y * floorDivide(x, y)); } public static final long floorDivide(long n, long d) { return ((n >= 0) ? (n / d) : (((n + 1L) / d) - 1L)); } public static final int floorDivide(int n, int d) { return ((n >= 0) ? (n / d) : (((n + 1) / d) - 1)); } public static final int floorDivide(int n, int d, int[] r) { if (n >= 0) { r[0] = n % d; return n / d; } int q = ((n + 1) / d) - 1; r[0] = n - (q * d); return q; } public static final int floorDivide(long n, int d, int[] r) { if (n >= 0) { r[0] = (int) (n % d); return (int) (n / d); } int q = (int) (((n + 1) / d) - 1); r[0] = (int) (n - (q * d)); return q; } }