77 * For example, <code>floorDivide(-1, 4)</code> returns -1 while 78 * -1/4 is 0. 79 * 80 * @param n the numerator 81 * @param d a divisor that must be greater than 0 82 * @return the floor of the quotient 83 */ 84 public static final int floorDivide(int n, int d) { 85 return ((n >= 0) ? 86 (n / d) : (((n + 1) / d) - 1)); 87 } 88 89 /** 90 * Divides two integers and returns the floor of the quotient and 91 * the modulus remainder. For example, 92 * <code>floorDivide(-1,4)</code> returns <code>-1</code> with 93 * <code>3</code> as its remainder, while <code>-1/4</code> is 94 * <code>0</code> and <code>-1%4</code> is <code>-1</code>. 95 * 96 * @param n the numerator 97 * @param d a divisor which must be > 0 98 * @param r an array of at least one element in which the value 99 * <code>mod(n, d)</code> is returned. 100 * @return the floor of the quotient. 101 */ 102 public static final int floorDivide(int n, int d, int[] r) { 103 if (n >= 0) { 104 r[0] = n % d; 105 return n / d; 106 } 107 int q = ((n + 1) / d) - 1; 108 r[0] = n - (q * d); 109 return q; 110 } 111 112 /** 113 * Divides two integers and returns the floor of the quotient and 114 * the modulus remainder. For example, 115 * <code>floorDivide(-1,4)</code> returns <code>-1</code> with 116 * <code>3</code> as its remainder, while <code>-1/4</code> is 117 * <code>0</code> and <code>-1%4</code> is <code>-1</code>. 118 * 119 * @param n the numerator 120 * @param d a divisor which must be > 0 121 * @param r an array of at least one element in which the value 122 * <code>mod(n, d)</code> is returned. 123 * @return the floor of the quotient. 124 */ 125 public static final int floorDivide(long n, int d, int[] r) { 126 if (n >= 0) { 127 r[0] = (int)(n % d); 128 return (int)(n / d); 129 } 130 int q = (int)(((n + 1) / d) - 1); 131 r[0] = (int)(n - (q * d)); 132 return q; 133 } 134 135 public static final long mod(long x, long y) { 136 return (x - y * floorDivide(x, y)); 137 } 138 139 public static final int mod(int x, int y) { 140 return (x - y * floorDivide(x, y)); | 77 * For example, <code>floorDivide(-1, 4)</code> returns -1 while 78 * -1/4 is 0. 79 * 80 * @param n the numerator 81 * @param d a divisor that must be greater than 0 82 * @return the floor of the quotient 83 */ 84 public static final int floorDivide(int n, int d) { 85 return ((n >= 0) ? 86 (n / d) : (((n + 1) / d) - 1)); 87 } 88 89 /** 90 * Divides two integers and returns the floor of the quotient and 91 * the modulus remainder. For example, 92 * <code>floorDivide(-1,4)</code> returns <code>-1</code> with 93 * <code>3</code> as its remainder, while <code>-1/4</code> is 94 * <code>0</code> and <code>-1%4</code> is <code>-1</code>. 95 * 96 * @param n the numerator 97 * @param d a divisor which must be {@literal > 0} 98 * @param r an array of at least one element in which the value 99 * <code>mod(n, d)</code> is returned. 100 * @return the floor of the quotient. 101 */ 102 public static final int floorDivide(int n, int d, int[] r) { 103 if (n >= 0) { 104 r[0] = n % d; 105 return n / d; 106 } 107 int q = ((n + 1) / d) - 1; 108 r[0] = n - (q * d); 109 return q; 110 } 111 112 /** 113 * Divides two integers and returns the floor of the quotient and 114 * the modulus remainder. For example, 115 * <code>floorDivide(-1,4)</code> returns <code>-1</code> with 116 * <code>3</code> as its remainder, while <code>-1/4</code> is 117 * <code>0</code> and <code>-1%4</code> is <code>-1</code>. 118 * 119 * @param n the numerator 120 * @param d a divisor which must be {@literal > 0} 121 * @param r an array of at least one element in which the value 122 * <code>mod(n, d)</code> is returned. 123 * @return the floor of the quotient. 124 */ 125 public static final int floorDivide(long n, int d, int[] r) { 126 if (n >= 0) { 127 r[0] = (int)(n % d); 128 return (int)(n / d); 129 } 130 int q = (int)(((n + 1) / d) - 1); 131 r[0] = (int)(n - (q * d)); 132 return q; 133 } 134 135 public static final long mod(long x, long y) { 136 return (x - y * floorDivide(x, y)); 137 } 138 139 public static final int mod(int x, int y) { 140 return (x - y * floorDivide(x, y)); |