I'm trying to round a number to the next smallest power of another number. The number I'm trying to round is always positive. I'm not particular on which direction it rounds, but I prefer downwards if possible.
I would like to be able to round towards arbitrary bases, but the ones I'm most concerned with at the moment is base 2 and fractional powers of 2 like 2^(1/2), 2^(1/4), and so forth. Here's my current algorithm for base 2. The log2 I multiply by is actually the inverse of log2:
double roundBaseTwo(double x)
{
return 1.0 / (1 << (int)((log(x) * log2))
}
Any help would be appreciated!
Edit: If it helps, the specified range of my input is always a non-zero, positive floating point number. It's almost always within the range of (0, 1], very rarely larger than 1.