@@ -156,7 +156,7 @@ public static int long2int(final long l) {
156
156
* @param x an integer smaller than or equal to 2<sup>30</sup>.
157
157
* @return the least power of two greater than or equal to the specified value.
158
158
*/
159
- public static int nextPowerOfTwo (int x ) {
159
+ public static int nextPowerOfTwo (final int x ) {
160
160
return 1 << (32 - Integer .numberOfLeadingZeros (x - 1 ));
161
161
}
162
162
@@ -167,7 +167,7 @@ public static int nextPowerOfTwo(int x) {
167
167
* @param x a long integer smaller than or equal to 2<sup>62</sup>.
168
168
* @return the least power of two greater than or equal to the specified value.
169
169
*/
170
- public static long nextPowerOfTwo (long x ) {
170
+ public static long nextPowerOfTwo (final long x ) {
171
171
return 1L << (64 - Long .numberOfLeadingZeros (x - 1 ));
172
172
}
173
173
@@ -180,8 +180,12 @@ public static long nextPowerOfTwo(long x) {
180
180
*/
181
181
public static int maxFill (final int n , final float f ) {
182
182
/* We must guarantee that there is always at least
183
- * one free entry (even with pathological load factors). */
184
- return Math .min ((int )Math .ceil (n * f ), n - 1 );
183
+ * one free entry (even with pathological load factors).
184
+ *
185
+ * The cast to double is essential to avoid a precision
186
+ * loss due to a cast to float before the call to Math.ceil.
187
+ */
188
+ return Math .min ((int )Math .ceil (n * (double )f ), n - 1 );
185
189
}
186
190
187
191
/** Returns the maximum number of entries that can be filled before rehashing.
@@ -192,8 +196,12 @@ public static int maxFill(final int n, final float f) {
192
196
*/
193
197
public static long maxFill (final long n , final float f ) {
194
198
/* We must guarantee that there is always at least
195
- * one free entry (even with pathological load factors). */
196
- return Math .min ((long )Math .ceil (n * f ), n - 1 );
199
+ * one free entry (even with pathological load factors).
200
+ *
201
+ * The cast to double is essential to avoid a precision
202
+ * loss due to a cast to float before the call to Math.ceil.
203
+ */
204
+ return Math .min ((long )Math .ceil (n * (double )f ), n - 1 );
197
205
}
198
206
199
207
/** Returns the least power of two smaller than or equal to 2<sup>30</sup> and larger than or equal to {@code Math.ceil(expected / f)}.
@@ -204,7 +212,11 @@ public static long maxFill(final long n, final float f) {
204
212
* @throws IllegalArgumentException if the necessary size is larger than 2<sup>30</sup>.
205
213
*/
206
214
public static int arraySize (final int expected , final float f ) {
207
- final long s = Math .max (2 , nextPowerOfTwo ((long )Math .ceil (expected / f )));
215
+ /*
216
+ * The cast to double is essential to avoid a precision
217
+ * loss due to a cast to float before the call to Math.ceil.
218
+ */
219
+ final long s = Math .max (2 , nextPowerOfTwo ((long )Math .ceil (expected / (double )f )));
208
220
if (s > (1 << 30 )) throw new IllegalArgumentException ("Too large (" + expected + " expected elements with load factor " + f + ")" );
209
221
return (int )s ;
210
222
}
@@ -216,6 +228,10 @@ public static int arraySize(final int expected, final float f) {
216
228
* @return the minimum possible size for a backing big array.
217
229
*/
218
230
public static long bigArraySize (final long expected , final float f ) {
219
- return nextPowerOfTwo ((long )Math .ceil (expected / f ));
231
+ /*
232
+ * The cast to double is essential to avoid a precision
233
+ * loss due to a cast to float before the call to Math.ceil.
234
+ */
235
+ return nextPowerOfTwo ((long )Math .ceil (expected / (double )f ));
220
236
}
221
237
}
0 commit comments