0

I'm sorry if I got the terminology wrong. Let me explain with an example.

Given a multiplier (we'll say 2.5), I need an equation or algorithm to find what power level a number resides in (preferably calculated, not a loop).

Level 0: < 2.5
Level 1: < 6.25 (2.5 ^ 2)
Level 2: < 15.625 (2.5 ^ 3)

And input of 8 would give an output of 15.625, as it is greater than or equal to 6.25 and less than 15.625.

Please let me know if my question is unclear. Thanks guys.

DaiBu
  • 103

2 Answers2

1

If $b$ is the base ("multiplier", $2.5$ in the example) and $x$ is the number to be tested, the next power of $b$ is $$b^{\left\lfloor\log_b x\right\rfloor+1}$$ where $\log_b x$ is the logarithm to base $b$ and $\lfloor x\rfloor$ is the floor function.

If you don't have access to arbitrary logarithms, use $$\log_b x = \frac{\log x}{\log b}$$ where the logarithm on the right can be whatever logarithm is available to you — typically either the natural logarithm, or the base 10 logarithm —, but it must be the same for the numerator and denominator.

Edit note: I corrected the formula after I noticed that when your number already is a power of the given base, you want the next one. The pre-edit formula would have given the same power for this case (so for example, $6.25$ would have resulted in $6.25$ with the old formula, but gives $15.625$ with the corrected one. Note that for any other numbers, both give the same result.

celtschk
  • 43,384
  • Thanks. Yeah, it's actually a tiny value that I'm working with. I just needed to know how to get the power level of a number. I ended up doing log(x / s * 2.5) / log(2.5) where s is the starting/standard value for x. It seems to work as expected. Please lmk if this isn't correct. – DaiBu Sep 11 '16 at 23:56
  • Well, $\log(x/s\cdot 2.5)/\log(2.5) = \log(x/s)/log(2.5)+1$, so assuming that the number whose exponent you want to determine is $x/s$, that is, except for the floor function, exactly the exponent in my formula. Note that in some programming languages (like C), at least for positive values assigning to an integer gives the same result as the floor function (assuming no overflow, of course); I guess that's what you do. In that case, your code is correct as long as $x/s\ge 1$. For $x/s<1$, under these assumptions you'd get an exponent that's too large by one, because truncation of negative … – celtschk Sep 12 '16 at 07:09
  • numbers is the ceiling function. Of course it also might be that you don't convert to an integer at all; in that case, you of course don't get the function you asked for; it may, of course, be that you get what you need anyway; there's no way to know without knowing what you do with that value. – celtschk Sep 12 '16 at 07:12
  • Yup, x / s will always be greater than or equal to 1. So I'm using the exact function that you gave me written in a different way? That's good cause I didn't fully understand the notation you provided. Sometimes you fumble around in the dark and just end up where you're trying to get to. Heh. Thanks. I appreciate the help. – DaiBu Sep 12 '16 at 08:37
  • Provided that my assumption was was right on that you convert to integer (and the integer conversion happens either in C,or on a language with the same conversion semantics), your implementation is for the values that occur the same as what I wrote, yes. BTW, what part of the notation did you not understand? – celtschk Sep 12 '16 at 17:43
  • The b[log b x] + 1. I only learned about logarithms after I posted this question and Arthur replied above. Your log x / log b got me to a place where I learned the rest and I understand it now. – DaiBu Sep 13 '16 at 00:26
1

I think the formula $2.5^{\lceil \log_{2.5}x \rceil}$ is the one you want. So for your example with $x=8$ we have,

$$2.5^{\lceil \log_{2.5}x \rceil} = 2.5^{\lceil \log_{2.5}8 \rceil} = 2.5^{\lceil 2.26941\dots \rceil} = 2.5^3 = 15.625.$$

EDIT: This formula might be a bit off in certain edge cases since you have $<$'s and not $\leq$'s. In particular, if $\log_{2.5}x$ is an integer, you would want to add $1$ to exponent. Whether or not you would want to do that is moreso based on the context of the problem you are trying to solve though.

benguin
  • 3,846
  • 1
    I'm sure this is correct, but I accepted celtschk's answer as he explained how to do the logs, which I didn't understand. Thanks for your time though. – DaiBu Sep 11 '16 at 23:59
  • @DaiBu That is fine, no problem. – benguin Sep 12 '16 at 00:01