Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #1475

Re: Exp() function reloaded

From "io_x" <a@b.c.invalid>
Newsgroups sci.math.num-analysis, alt.lang.asm, comp.programming
References <jmdbuh$fql$1@dont-email.me> <4F8A5BF2.17C5@mindspring.com>
Subject Re: Exp() function reloaded
Date 2012-04-16 09:01 +0200
Message-ID <4f8bc299$0$1377$4fafbaef@reader2.news.tin.it> (permalink)
Organization TIN.IT (http://www.tin.it)

Cross-posted to 3 groups.

Show all headers | View raw


"pete" <pfiland@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


Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Exp() function reloaded hopcode <hopcode@invalid.de> - 2012-04-15 04:31 +0200
  Re: Exp() function reloaded pete <pfiland@mindspring.com> - 2012-04-15 01:26 -0400
    Re: Exp() function reloaded hopcode <hopcode@invalid.de> - 2012-04-15 12:18 +0200
      Re: Exp() function reloaded pete <pfiland@mindspring.com> - 2012-04-15 09:30 -0400
    Re: Exp() function reloaded "io_x" <a@b.c.invalid> - 2012-04-16 09:01 +0200
      Re: Exp() function reloaded "io_x" <a@b.c.invalid> - 2012-04-16 11:50 +0200
        Re: Exp() function reloaded "io_x" <a@b.c.invalid> - 2012-04-16 15:46 +0200
          Re: Exp() function reloaded "io_x" <a@b.c.invalid> - 2012-04-16 16:07 +0200
            Re: Exp() function reloaded "io_x" <a@b.c.invalid> - 2012-04-17 09:36 +0200
              Re: Exp() function reloaded phreda <pabloreda@gmail.com> - 2012-04-19 19:43 -0700
                Re: Exp() function reloaded hopcode <hopcode@invalid.de> - 2012-04-24 14:48 +0200
                Re: Exp() function reloaded phreda <pabloreda@gmail.com> - 2012-04-25 12:36 -0700
        Re: Exp() function reloaded pete <pfiland@mindspring.com> - 2012-04-16 18:12 -0400

csiph-web