Get exponent from number and base?

Started by
5 comments, last by Conner McCloud 18 years, 10 months ago
Is there a way to get an exponent from a number and a base. For example, passing 32 and 2 would result in 5 because 2 to the power of 5=32. Thanks
Advertisement
this works it seems

#include <iostream>
#include <math.h>
using namespace std;

int main(){

int tot = //number you want to find exponent of
int curr = 0; //keeps track of values
int x;
for(x = 0; curr <= tot; x++){
curr += pow(2,x);
}
x = x - 1;
cout << x << endl;

}
Simple algebra: log(x)/log(y) or ln(x)/ln(y) if you'd prefer :P

So your example would be log(32)/log(2) = 5
Quote:Original post by load_bitmap_file
Simple algebra: log(x)/log(y) or ln(x)/ln(y) if you'd prefer :P

So your example would be log(32)/log(2) = 5


Log returns base-10 logarithm?
Quote:Original post by Raeldor
Log returns base-10 logarithm?


Doesn't matter, as long as you do log(x)/log(y), it will work.
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
Quote:Original post by chaosgame
Quote:Original post by Raeldor
Log returns base-10 logarithm?


Doesn't matter, as long as you do log(x)/log(y), it will work.


Is this one of these mathematical mysteries like PI? :P
Quote:Original post by Raeldor
Is this one of these mathematical mysteries like PI? :P

No. In fact, it's pretty trivial to prove if you try.

CM

This topic is closed to new replies.

Advertisement