Hey, /sci/, I need a function that allows me to do exponentiations.
a^b
I already did it, but it only works raising any base with ONLY whole numbers. I need to operate bases with real numbers.
Here's the code I did for only whole numbers at the exponent:
double pow(double b,double e){
double x=b;
for(e;e>1;e--) {
x*=b;
}
return x;
}
Sorry about my english.
http://opensource.apple.com/source/Libm/Libm-315/Source/ARM/powf.c
>>8012483
Too advanced for me.
>>8012499
well it's a real-world power function
>>8012471
>double pow(double b,double e){
>double x=b;
>for(e;e>1;e--) {
>x*=b;
>}
[math]ARE ~ YOU ~ FUCKING ~ RETARDED?[/math] Do you multiple numbers like this:
double mult(double b,double e){
double x=b;
for(e;e>1;e--) {
x+=b;
}
return x;
}
Do you add numbers like this:
double add(double b,double e){
double x=b;
for(e;e>1;e--) {
x++;
}
return x;
}
Why are you exponenting like a fucking tard?
>>8012516
I don't see the problem with the code you posted?
>>8012471
double pow(double x, double y)
C standard in math.h
>>8012536
a^b = exp( b log(a) ) ?
More seriously,
b = n + r where n is an integer, and 0<r<1
so a^b = a^n a^r
You have code for a^n, so you just need a^r.
Use a for loop and use Newton's Method to approximate the value quickly
>>8012525
I think he mean e can be a floating point number
>>8012541
a^b is the bitwise exclusive OR of a and b
for example if a is 010010 and b is 101111 then a^b is 111101
>>8012471
i have one that works for any complex number raised to any complex number
>>8012525
>typical CS major
>>8012471
The best you can do I believe is to use the taylor series for exponential functions. Obviously not the entire expression because that would be impossible but just precise enough that it fits your I presume scientific purposes.
I think that is how calculators do it but I could be wrong. I mean, in what other way?
>>8012649
Log and exponent look up tables are faster given that you have enough space
If I were coding a standalone function for a microcontroller tooth, I would also use Taylor. Or I needed a complex exponential.
>the "simple" way
const unsigned precision=40;
double exponential(unsigned n, double x) //exp(x)
{
double sum = 1.0;
for (int i = n - 1; i > 0; --i ){
sum = 1+x*sum/i;
}
return sum;
}
double log_natural_frac(unsigned n, double x){ //ln(1-x) & |x|<1
double sum=0;
for(int i=n-1;i>0;--i){
sum*=x;
sum+=x/i;
}
return -sum;
}
double log_natural(unsigned n, double x){
double e=exponential(n, 1), log=0;
int power=1;
double ePow=e;
while(ePow<x){
ePow*=e;
power<<=1;
}
ePow/=e;
power>>=1;
while(power>0){
if(ePow<x){
x/=ePow;
log+=power;
}
ePow/=e;
power>>=1;
}
if(x>1){
x/=e;
++log;
}
return log+log_natural_frac(n,1-x);
}
double power(double b, unsigned long long n){
double result=1, bPow=b;
while(n){
if(n&1){
result*=bPow;
}
bPow*=bPow;
n>>=1;
}
return result;
}
double power(double b, double e){
if(e<0){
return 1/power(b,-e);
}
double result=power(b, static_cast<unsigned long long>(e));
e-=static_cast<unsigned long long>(e);
if(e==0){
return result;
}
return result*exponential(precision, e*log_natural(precision, b));
}