-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
41 lines (34 loc) · 1.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var Stopwatch = require('node-stopwatch').Stopwatch;
var monte_carlo = require('./samples/monte-carlo-pi/monte-carlo-pi').run;
var args = process.argv.slice(2);
if (args.length < 1) {
console.error("Usage: node app.js {sample-to-run} {required sample arguments}");
}
var sample = args[0];
switch (sample) {
case 'monte-carlo-pi':
if (args.length < 2) {
console.error("Usage: node app.js monte-carlo-pi {number of darts to throw}");
break;
}
var verbose = false;
if (args.length > 2) {
var optSwitch = args[2];
if (optSwitch == '-v') {
verbose = true;
}
}
var n = parseInt(args[1], 10);
var stopwatch = Stopwatch.create();
monte_carlo(n, function(estimate) {
stopwatch.stop();
console.log('Estimate value of pi: ', estimate);
console.log('Accuracy (estimate - pi): ', estimate - Math.PI);
if (verbose) {
console.log('Elapsed time: ' + stopwatch.elapsedMilliseconds + 'ms');
}
});
break;
default:
break;
}