-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmarks: add Ramanujan's formula for calculating pi
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.