C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Classes | Functions
exprtk_wiener_process_pi.cpp File Reference
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_wiener_process_pi.cpp:

Go to the source code of this file.

Classes

struct  rnd_01< T >
 

Functions

template<typename T >
void wiener_process_pi ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 82 of file exprtk_wiener_process_pi.cpp.

83{
84 wiener_process_pi<double>();
85 return 0;
86}

◆ wiener_process_pi()

template<typename T >
void wiener_process_pi ( )

Definition at line 45 of file exprtk_wiener_process_pi.cpp.

46{
47 typedef exprtk::symbol_table<T> symbol_table_t;
48 typedef exprtk::expression<T> expression_t;
49 typedef exprtk::parser<T> parser_t;
50
51 const std::string wiener_process_pi_program =
52 " var w[10^4] := [0]; "
53 " "
54 " for (var i := 0; i < w[]; i += 1) "
55 " { "
56 " var x[10^4] := [(rnd_01 < 1 / 2) ? -1 : 1]; "
57 " w[i] := sum(x); "
58 " } "
59 " "
60 " (2 * w[]) / avg(abs(w))^2; ";
61
62 rnd_01<T> rnd01;
63
64 symbol_table_t symbol_table;
65 symbol_table.add_function("rnd_01",rnd01);
66
67 expression_t expression;
68 expression.register_symbol_table(symbol_table);
69
70 parser_t parser;
71 parser.compile(wiener_process_pi_program,expression);
72
73 const T approximate_pi = expression.value();
74
75 const T real_pi = T(3.141592653589793238462643383279502); // or close enough...
76
77 printf("pi ~ %20.17f\terror: %20.17f\n",
78 approximate_pi,
79 std::abs(real_pi - approximate_pi));
80}