[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 33 KB, 600x400, c-logo.png [View same] [iqdb] [saucenao] [google]
8012471 No.8012471 [Reply] [Original]

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.

>> No.8012483

http://opensource.apple.com/source/Libm/Libm-315/Source/ARM/powf.c

>> No.8012499

>>8012483
Too advanced for me.

>> No.8012505

>>8012499
well it's a real-world power function

>> No.8012516

>>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?

>> No.8012525

>>8012516
I don't see the problem with the code you posted?

>> No.8012531

>>8012471
double pow(double x, double y)
C standard in math.h

>> No.8012536

>>8012516
Because I'm learning to do that...?
What's wrong with my code?


>>8012531
I'm not allowed to use math.h, only iostream, iomanip and stdlib.h. I HAVE to do the function.

>> No.8012541

>>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.

>> No.8012584

Use a for loop and use Newton's Method to approximate the value quickly

>> No.8012610

>>8012525
I think he mean e can be a floating point number

>> No.8012612

>>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

>> No.8012614

>>8012471
i have one that works for any complex number raised to any complex number

>> No.8012625
File: 70 KB, 694x801, 1454454175818.jpg [View same] [iqdb] [saucenao] [google]
8012625

>>8012525
>typical CS major

>> No.8012628

>>8012536
>What's wrong with my code?

It's exponential in e

>>8012525

All three functions take exponential time in e.

>>8012612

math syntax retard, not c++.

>> No.8012649

>>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?

>> No.8012659

>>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.

>> No.8012747 [DELETED] 

>>8012659
>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));
}

>> No.8012751

>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));
}