Andersen said:
Hope you had a little fun there.
Sure, nothing in the group's charter prohibits having a bit of fun.
And as a general hint, playing with then numbers and some formulas when
you have a math problem at hand doesn't hurt. It gives you some feel for
the problem.
I have an application where I know the
numbers are powers of some base k,
k being a positive integer? Why didn't you tell us this upfront? You
could e.g. handle that problem with a lookup table. E.g for a base 2 a
table of 31 integers if you really don't trust log():
final static int logs2[] = {
0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x00000100, 0x00000200, 0x00000400, 0x00000800,
0x00001000, 0x00002000, 0x00004000, 0x00008000,
0x00010000, 0x00020000, 0x00040000, 0x00080000,
0x00100000, 0x00200000, 0x00400000, 0x00800000,
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000
};
final static int logs3[] = {
1, 3, 9, 27,
...
};
private int binSearch(int values[], int e) throws ImpreciseException
{
int l = 0;
int r = values.length - 1;
int p;
while(l <= r) {
p = (l + r) / 2;
if(e == values[p]) return p;
if(e < values[p]) {
r = p - 1;
} else {
l = p + 1;
}
}
throw new ImpreciseException();
}
public int preciseLog2(int e) throws ImpreciseException
{
return binSearch(logs2, e);
}
public int preciseLog3(int e) throws ImpreciseException
{
return binSearch(logs3, e);
}
/Thomas