[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Hey, /sci/, I need a function that allows me to do exponentiations.

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 18
Thread images: 2

File: c-logo.png (33KB, 600x400px) Image search: [Google]
c-logo.png
33KB, 600x400px
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
>>
>>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.
>>
>>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
>>
File: 1454454175818.jpg (70KB, 694x801px) Image search: [Google]
1454454175818.jpg
70KB, 694x801px
>>8012525
>typical CS major
>>
>>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++.
>>
>>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));
}
Thread posts: 18
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.