Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation to calculate an estimate of the number π (Pi). More...
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
Namespaces | |
namespace | math |
for IO operations | |
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 π
int main | ( | void | ) |
|
static |
Self-test implementations.