Skip to content

Commit

Permalink
benchmarks: add Ramanujan's formula for calculating pi
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Aug 14, 2020
1 parent 439af1d commit 62f20d0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/benchmarks/ramanujan_pi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <math.h>

#define WASM_EXPORT __attribute__((visibility("default")))

static unsigned factorial(unsigned n)
{
unsigned ret = 1;
for (unsigned i = n; i > 1; --i)
ret *= i;
return ret;
}

static double ramanujan_pi(unsigned n)
{
// This dumb implementation of Ramanujan series calculates 1/pi
double ret = 0.0;
for (unsigned k = 0; k < n; k++) {
double a = factorial(4 * k) / pow(factorial(k), 4.0);
double b = (26390.0 * k + 1103.0) / pow(396.0, 4 * k);
ret += a * b;
}

// Some intentional f32 -> f64 conversion here
ret *= (2.0f * sqrt(2.0)) / pow(99.0f, 2.0);

// Return pi
return 1.0 / ret;
}

WASM_EXPORT unsigned long long ramanujan(unsigned n)
{
// Display all 16 digits of double precision as a 64-bit integer
return ramanujan_pi(n) * 10000000000000000ULL;
}
14 changes: 14 additions & 0 deletions test/benchmarks/ramanujan_pi.inputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pi_10_runs
ramanujan
i:I
10

31415926535897932

pi_30_runs
ramanujan
i:I
30

31415926535897932

Binary file added test/benchmarks/ramanujan_pi.wasm
Binary file not shown.

0 comments on commit 62f20d0

Please sign in to comment.