Arbitrary precision (big number) library port for Arduino

Just as another example, you can calculate "e" to around 100 digits like this:

#include "BigNumber.h"

static int DIGITS = 103;

void print_bignum (bc_num x)
{
  char *s=bc_num2str(x);
  Serial.println (s);
  free(s);
}

void setup ()
{
  Serial.begin (115200);
  Serial.println ();
  Serial.println ();
  bc_init_numbers ();  // initialize library

  char buf [10]; 

  // some big numbers
  bc_num n= NULL, e = NULL, one = NULL;

  // the number: 1
  bc_str2num (&one, "1", DIGITS);

  e = bc_copy_num (one);
  n = bc_copy_num (one);

  for (int j = 1; j <= 200; j++)
  { 
    bc_num E = bc_copy_num (e);
    bc_num i = NULL;
    sprintf (buf, "%i", j);
    bc_str2num(&i, buf, DIGITS);
    bc_multiply (i, n, &n, DIGITS);  // n is i! (factorial of i)
    bc_free_num (&i);
    bc_num inverse = NULL;
    bc_divide (one, n, &inverse, DIGITS);
    bc_add (inverse, e, &e, DIGITS);
    bc_free_num (&inverse);
    if (bc_compare (e, E) == 0)  // sequence has converged
      break;
    bc_free_num (&E);
  }

  print_bignum (e);
} // end of setup

void loop () { }

Output:

2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274240

Time to execute: around 4 seconds.

(edit)

Theory:

E can be calculated as:

1 + (1/1!) + (1/2!) + (1/3!) ...

That's why the code above is calculating running factorials, and adding the inverse of the factorial to the running total. The code breaks out of the loop when, for the desired number of decimal places, the calculated value doesn't change. That is, at this level of precision, the new amount added does not change the result.