Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
pete
Posts:
190
Registered:
12/11/04
|
|
Re: Exp() function reloaded
Posted:
Apr 15, 2012 1:26 AM
|
|
hopcode wrote: > > Hi, > here my method for the exponentianal function e^x . > it is a mix of 2 fundamentals: > - Taylor series running at a 10 steps for decimals -1.0 < x < +1.0 > - a couple of basic rules of the logarithms. > it is ~40 lines of code (my Taylors's exp() code + main routine) > it accepts only +numbers for now, and makes no exaustive check > on the floats. for those and negative values i leave it to the > reader's creativity ( being e^-x essentially 1 / e^x ).
I've code Taylors's exp(x) for both positive and negative x in C, using -1.0 < x < +1.0, but without making use of e^-x being essentially 1 / e^x.
/* BEGIN new.c output */
fs_expl(20.3) is 654904512.153239 fs_expl(20.3) - 654904512.153230 is 8.583069e-006
fs_expl(-20.3) is 1.526940e-009 fs_expl(-20.3) - 1.526940e-009 is 1.591266e-016
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
long double fs_expl(long double x) { long unsigned n, square; long double b, e, old; for (square = 0; x > 1; x /= 2) { ++square; } while (-1 > x) { ++square; x /= 2; } e = b = n = 1; do { b /= n++; b *= x; e += b; b /= n++; b *= x; old = e; e += b; } while (e > old); while (square-- != 0) { e *= e; } return e; }
int main(void) { puts("/* BEGIN new.c output */\n"); printf("fs_expl(20.3) is %lf\n", fs_expl(20.3)); printf("fs_expl(20.3) - 654904512.153230 is %le\n\n", fs_expl(20.3) - 654904512.153230);
printf("fs_expl(-20.3) is %le\n", fs_expl(-20.3)); printf("fs_expl(-20.3) - 1.526940e-009 is %le\n\n", fs_expl(-20.3) - 1.526940e-009);
puts("/* END new.c output */"); return 0; }
/* END new.c */
-- pete
|
|
|
|