Dan894
11-02-2006, 09:57
What's the best way to do this. I've heard make a look up table (which I'm not entirely sure how to do anyway, I've never done arrays or array-like things in C), and Taylor polynomials. I've been trying the Taylor approach, but encounter this crude reality that most computers do NOT have infinite memory.
Is defining an integral exponential function like this (recursively) generally a bad idea?
float exp(float base, int exponent) {
if(exponent < 0)
return exp(base, -1*exponent)
else if(exponent == 0)
return 1;
else
return exp(base, exponent-1)
}
I did a similar thing with factorial...
unsigned long factorial (unsigned character n) {
if(n==0)
return 1;
else
return n*factorial(n-1);
}
I doubt I'd ever be using that for something bigger than 12! or so.
Of course, since I had some debugging to do, I wound up defining the trig functions using a much more brute force mentality:
float sin(float x) {
return x-(x*x*x)/6+(x*x*x*x*x)/120-(x*x*x*x*x*x*x)/5040+(x*x*x*x*x*x*x*x*x)/362880;
}
float cos(float x) {
return 1-(x*x)/2+(x*x*x*x)/24-(x*x*x*x*x*x)/720+(x*x*x*x*x*x*x*x)/40320;
}
Then I defined the other four using ratios... and the arctrig...
float arcsin(float x) {
return x + x*x*x/6 + 3*x*x*x*x*x/8 + 135*x*x*x*x*x*x*x/48 + 1215*x*x*x*x*x*x*x*x*x/384;
}
float arctan(float x) {
if(-1 < x && x < 1)
return x-x*x*x/3+x*x*x*x*x/5-x*x*x*x*x*x*x/7+x*x*x*x*x*x*x*x*x/9;
else if(x >= 1)
return pi/2-1/x+1/(3*x*x*x)-1/(5*x*x*x*x*x)+1/(7*x*x*x*x*x*x*x)-1/(9*x*x*x*x*x*x*x*x*x);
else
return -pi/2-1/x+1/(3*x*x*x)-1/(5*x*x*x*x*x)+1/(7*x*x*x*x*x*x*x)-1/(9*x*x*x*x*x*x*x*x*x);
}
And the remaining ones I defined using triangle identities.
Around the term x*x*x*x*x*x*x*x*x/362880, I start getting an error message like
Hence I just give up the accuracy and only get accurate to the 1.6^8/40320 or whichever term I gave up at. And I still don't trust that this code will, you know, actually work. (I also wound up defining
float pi = 3.141592653589793238462548838279; )
Is defining an integral exponential function like this (recursively) generally a bad idea?
float exp(float base, int exponent) {
if(exponent < 0)
return exp(base, -1*exponent)
else if(exponent == 0)
return 1;
else
return exp(base, exponent-1)
}
I did a similar thing with factorial...
unsigned long factorial (unsigned character n) {
if(n==0)
return 1;
else
return n*factorial(n-1);
}
I doubt I'd ever be using that for something bigger than 12! or so.
Of course, since I had some debugging to do, I wound up defining the trig functions using a much more brute force mentality:
float sin(float x) {
return x-(x*x*x)/6+(x*x*x*x*x)/120-(x*x*x*x*x*x*x)/5040+(x*x*x*x*x*x*x*x*x)/362880;
}
float cos(float x) {
return 1-(x*x)/2+(x*x*x*x)/24-(x*x*x*x*x*x)/720+(x*x*x*x*x*x*x*x)/40320;
}
Then I defined the other four using ratios... and the arctrig...
float arcsin(float x) {
return x + x*x*x/6 + 3*x*x*x*x*x/8 + 135*x*x*x*x*x*x*x/48 + 1215*x*x*x*x*x*x*x*x*x/384;
}
float arctan(float x) {
if(-1 < x && x < 1)
return x-x*x*x/3+x*x*x*x*x/5-x*x*x*x*x*x*x/7+x*x*x*x*x*x*x*x*x/9;
else if(x >= 1)
return pi/2-1/x+1/(3*x*x*x)-1/(5*x*x*x*x*x)+1/(7*x*x*x*x*x*x*x)-1/(9*x*x*x*x*x*x*x*x*x);
else
return -pi/2-1/x+1/(3*x*x*x)-1/(5*x*x*x*x*x)+1/(7*x*x*x*x*x*x*x)-1/(9*x*x*x*x*x*x*x*x*x);
}
And the remaining ones I defined using triangle identities.
Around the term x*x*x*x*x*x*x*x*x/362880, I start getting an error message like
Hence I just give up the accuracy and only get accurate to the 1.6^8/40320 or whichever term I gave up at. And I still don't trust that this code will, you know, actually work. (I also wound up defining
float pi = 3.141592653589793238462548838279; )