File: pi_est.cc

package info (click to toggle)
node-nan 2.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: cpp: 8,007; ansic: 1,604; javascript: 1,588; makefile: 134; sh: 34
file content (63 lines) | stat: -rw-r--r-- 1,694 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
 * Copyright (c) 2018 NAN contributors
 *
 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
 ********************************************************************/

#include <cstdlib>
#include "pi_est.h"  // NOLINT(build/include_subdir)

/*
Estimate the value of π by using a Monte Carlo method.
Take `points` samples of random x and y values on a
[0,1][0,1] plane. Calculating the length of the diagonal
tells us whether the point lies inside, or outside a
quarter circle running from 0,1 to 1,0. The ratio of the
number of points inside to outside gives us an
approximation of π/4.

See https://en.wikipedia.org/wiki/File:Pi_30K.gif
for a visualization of how this works.
*/

inline int randall(unsigned int *p_seed) {
// windows has thread safe rand()
#ifdef _WIN32
  return rand();  // NOLINT(runtime/threadsafe_fn)
#else
  return rand_r(p_seed);
#endif
}

double Estimate (int points) {
  int i = points;
  int inside = 0;
  unsigned int randseed = 1;

#ifdef _WIN32
  srand(randseed);
#endif

  // unique seed for each run, for threaded use
  unsigned int seed = randall(&randseed);

#ifdef _WIN32
  srand(seed);
#endif

  while (i-- > 0) {
    double x = randall(&seed) / static_cast<double>(RAND_MAX);
    double y = randall(&seed) / static_cast<double>(RAND_MAX);

    // x & y and now values between 0 and 1
    // now do a pythagorean diagonal calculation
    // `1` represents our 1/4 circle
    if ((x * x) + (y * y) <= 1)
      inside++;
  }

  // calculate ratio and multiply by 4 for π
  return (inside / static_cast<double>(points)) * 4;
}