Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Exp() function reloaded
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
io_x  
View profile  
 More options Apr 16 2012, 3:01 am
Newsgroups: sci.math.num-analysis, alt.lang.asm, comp.programming
From: "io_x" <a...@b.c.invalid>
Date: Mon, 16 Apr 2012 09:01:10 +0200
Local: Mon, Apr 16 2012 3:01 am
Subject: Re: Exp() function reloaded

"pete" <pfil...@mindspring.com> ha scritto nel messaggio
news:4F8A5BF2.17C5@mindspring.com...

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

Pet's one is better and fast of what i use...

#include <stdio.h>
#include <math.h>

#define  u32   unsigned
#define  u64   unsigned __int64
#define  i32   int
#define  u16   unsigned short
#define  u8    unsigned char
#define  i8    char
#define  i16   short
#define  sdC  __stdcall

#define  F   for
#define  R   return
#define  W   while
#define  G   goto
#define  P   printf

double ke=2.7182818284590452353602874713527;
int    con;

long double fs_expl(long double x)
{
    long unsigned n, square;
    long double b, e, old;

    con=0;
    for (square = 0; x > 1; x /= 2) {++con;
        ++square;
    }
    while (-1 > x) {++con;
        ++square;
        x /= 2;
    }
    e = b = n = 1;
    do {++con;
        b /= n++;
        b *= x;
        e += b;
        b /= n++;
        b *= x;
        old = e;
        e += b;
    } while (e > old);
    while (square-- != 0) {++con;
        e *= e;
    }
    return e;

}

// alcune cose [quelle giuste]
// provengono da "Satoshi Tomabechi"
// per il resto non so 100% come funziona...
// 0 per errore altrimenti ritorna il numero di cicli
u32  mexp(double* r, double espon)
{double  ip, fp, t, x;
 u32          v, w, i;

 i=0;
 if(r==0)         R   0;
 *r=0.0;
 if(espon==0.0) {*r=1.0;
                  R   1;}
 else if(espon<0) x=-espon;
 else             x= espon;
 fp=modf(x, &ip);  t=ke;
 if(ip>0xFFFFFFF) R   0;
 F(w=1, v=ip; w<v; ++w)
         {++i; t=t*ke;}
 ip=t;
 v=2; t=1+fp; x=fp;
 F(;;){++i;
       x= x*(fp/v);
       if(x==0.0)  break;
       t+=x; ++v;
      }
 t*=ip;
 if(espon>=0) *r=    t;
 else         *r=1.0/t;
 R   i;

}

int  main(void)
{long  double   xx, dd;
 double     d, r, x, y;
 u32                 i;
 dd=12.1929292992; d=dd;
 x=exp(d); i=mexp(&y,d);
 P("exp(%.20f)=%.20f, %.20f  i=%d\n", d, x, y, i);
 xx=fs_expl(dd);
 x=xx;
 P("exp(%.20f)=%.20f i=%d\n", d, x, con);
 R  0;

}

exp(12.19292929919999935000)=197388.53005457221300000000,
                             197388.53005457215480000000 i=144
exp(12.19292929919999935000)=197388.53005457221300000000 i=18

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.