user
user

Reputation: 81

Run time errors while computing e^x

Continuing from the question I posted before How to increase precision for function e^x

I made few changes in my code by taking advices given

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

long double exponential(long double x, long double n, long double p)
{
    long double i = 1;

    while (n > 0) // loop stops when becomes less than 0
        i = i + (x / p) * (exponential(x, n - 0.0001, p + 1));

    if (n < 0) // when n reaches 0 or becomes less than zero value of i will be returned
        return i;
}

int main()
{
    long double p, x, n;
    scanf("%Lf", &x);
    printf("math.h e^x =     %lf\n", exp(x));
    printf("calculated e^x = %Lf\n", exponential(x, 1, 1));
    return 0;
}

But I am not getting any output its just giving run time error(http://codepad.org/jIKoYGFC) and I don't know why . Please can some one help me why I am getting these errors

Upvotes: 1

Views: 83

Answers (2)

Gerhard
Gerhard

Reputation: 7069

Created a version using LibGMP out of curiosity to see how this will improve the outcome.

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

#define BITS 128

The line above mean mantissa is 128 bits and exponent of 64 bits. This is my GMP based function:

void gmp_exponential(mpf_t * i, double x, double n, int p)
{
    mpf_t ii;
    mpf_init(ii);    
    mpf_set_d(ii,1);
    
    if (n > 0){
      mpf_t a,b,c;

      gmp_exponential(&ii, x, n - 0.0001, p + 1);
      
      mpf_inits (a, b, c, NULL);  
      mpf_set_d(a,x);
      mpf_div_ui(b, a, p) ;
      mpf_mul(c, b, ii);
      mpf_add (*i,*i,c);
      
      mpf_clears(a,b,c,NULL);
    }
    mpf_clear(ii);
}

Reusing the function from WhozCraig for comparison:

long double exponential(long double x, long double n, long double p)
{
    return 1 + ((n > 0) ? (x / p) * exponential(x, n - 0.0001, p + 1) : 0);
}

And code to run it all:

int main()
{
  double x = 30.0;
  
  mpf_t i;
  mpf_init2 (i, BITS);    
  mpf_set_d(i,1);
  gmp_exponential(&i, x, 1, 1);
  
  printf     ("math.h e^x              =  %Lf\n", expl(x));
  gmp_printf ("calculated e^x with mpf =  %.*Ff\n", 6, i);
  printf     ("calculated e^x          =  %Lf\n", exponential(x, 1, 1));
  
  mpf_clear(i);
  return 0;
}

Output:

math.h e^x              =  10686474581524.462147
calculated e^x with mpf =  10686474581524.462143
calculated e^x          =  10686474581524.462149

Upvotes: 0

WhozCraig
WhozCraig

Reputation: 66244

That loop is completely bogus. You're not writing an iterative function (where it would make more sense). Further you have an edge case of zero returning undefined content.

Though I do not recommend floating point for loop control, nor do I advise thousands of invocations into recursive calls, your code should be more like

long double exponential(long double x, long double n, long double p)
{
    long double i = 1;
    if (n > 0)
        i += (x / p) * (exponential(x, n - 0.0001, p + 1));
    return i;
}

which ultimately is just this:

long double exponential(long double x, long double n, long double p)
{
    return 1 + ((n > 0) ? (x / p) * exponential(x, n - 0.0001, p + 1) : 0);
}

Fixing that (either way):

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

long double exponential(long double x, long double n, long double p)
{
    return 1 + ((n > 0) ? (x / p) * exponential(x, n - 0.0001, p + 1) : 0);
}

int main()
{
    long double x = 5;
    printf("math.h e^x =     %Lf\n", expl(x));
    printf("calculated e^x = %Lf\n", exponential(x, 1, 1));
    return 0;
}

Output

math.h e^x =     148.413159
calculated e^x = 148.413159

Upvotes: 3

Related Questions