TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation to calculate an estimate of the number π (Pi). More...
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | math |
for assert | |
Typedefs | |
using | math::Point |
structure of points containing two numbers, x and y, such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1. | |
Functions | |
double | math::approximate_pi (const std::vector< Point > &pts) |
This function uses the points in a given vector 'pts' (drawn at random) to return an approximation of the number π. | |
static void | tests () |
Self-test implementations. | |
int | main () |
Main function. | |
Implementation to calculate an estimate of the number π (Pi).
We take a random point P with coordinates (x, y) such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1. If x² + y² ≤ 1, then the point is inside the quarter disk of radius 1, else the point is outside. We know that the probability of the point being inside the quarter disk is equal to π/4 double approx(vector<Point> &pts) which will use the points pts (drawn at random) to return an estimate of the number π
Definition in file approximate_pi.cpp.
int main | ( | void | ) |
Main function.
Definition at line 80 of file approximate_pi.cpp.
|
static |
Self-test implementations.
Definition at line 62 of file approximate_pi.cpp.