LUCENE-3037: calculate log with integer math

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1095147 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2011-04-19 17:51:10 +00:00
parent 8906ff3cc2
commit 4a20fa5782
2 changed files with 12 additions and 4 deletions

View File

@ -184,10 +184,19 @@ public abstract class MultiLevelSkipListReader {
}
}
/** returns x == 0 ? 0 : Math.floor(Math.log(x) / Math.log(base)) */
static int log(int x, int base) {
int ret = 0;
while (x >= base) {
x /= base;
ret++;
}
return ret;
}
/** Loads the skip levels */
private void loadSkipLevels() throws IOException {
// TODO: would be preferable to use integer math here instead.
numberOfSkipLevels = docCount == 0 ? 0 : (int) Math.floor(StrictMath.log(docCount) / StrictMath.log(skipInterval[0]));
numberOfSkipLevels = log(docCount, skipInterval[0]);
if (numberOfSkipLevels > maxNumberOfSkipLevels) {
numberOfSkipLevels = maxNumberOfSkipLevels;
}

View File

@ -61,8 +61,7 @@ public abstract class MultiLevelSkipListWriter {
this.skipInterval = skipInterval;
// calculate the maximum number of skip levels for this document frequency
// TODO: would be preferable to use integer math here instead.
numberOfSkipLevels = df == 0 ? 0 : (int) Math.floor(StrictMath.log(df) / StrictMath.log(skipInterval));
numberOfSkipLevels = MultiLevelSkipListReader.log(df, skipInterval);
// make sure it does not exceed maxSkipLevels
if (numberOfSkipLevels > maxSkipLevels) {