C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_montecarlo_e.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Approximation of e via Monte-Carlo Method *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <cstdlib>
22
#include <ctime>
23
#include <string>
24
25
#include "
exprtk.hpp
"
26
27
28
template
<
typename
T>
29
struct
rnd_01
:
public
exprtk::ifunction
<T>
30
{
31
using
exprtk::ifunction<T>::operator();
32
33
rnd_01
() :
exprtk
::
ifunction
<T>(0)
34
{ ::srand(
static_cast<
unsigned
int
>
(time(NULL))); }
35
36
inline
T
operator()
()
37
{
38
// Note: Do not use this in production
39
// Result is in the interval [0,1)
40
return
T(::rand() / T(RAND_MAX + 1.0));
41
}
42
};
43
44
template
<
typename
T>
45
void
monte_carlo_e
()
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 monte_carlo_e_program =
52
" var max_samples := 10^8; "
53
" var trials := 0; "
54
" "
55
" for (var i := 0; i < max_samples; i += 1) "
56
" { "
57
" var rand_sum := 0; "
58
" repeat "
59
" rand_sum += rnd_01; "
60
" trials += 1; "
61
" until (rand_sum > 1); "
62
" }; "
63
" "
64
" trials / max_samples; "
;
65
66
rnd_01<T>
rnd01;
67
68
symbol_table_t symbol_table;
69
symbol_table.add_function(
"rnd_01"
,rnd01);
70
71
expression_t expression;
72
expression.register_symbol_table(symbol_table);
73
74
parser_t parser;
75
parser.compile(monte_carlo_e_program,expression);
76
77
const
T approximate_e = expression.value();
78
79
const
T real_e = T(2.718281828459045235360287471352662);
// or close enough...
80
81
printf(
"e ~ %20.17f\terror: %20.17f\n"
,
82
approximate_e,
83
std::abs(real_e - approximate_e));
84
}
85
86
int
main
()
87
{
88
monte_carlo_e<double>
();
89
return
0;
90
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::ifunction
Definition
exprtk.hpp:19861
exprtk::ifunction::ifunction
ifunction(const std::size_t &pc)
Definition
exprtk.hpp:19864
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
monte_carlo_e
void monte_carlo_e()
Definition
exprtk_montecarlo_e.cpp:45
main
int main()
Definition
exprtk_montecarlo_e.cpp:86
exprtk
Definition
exprtk.hpp:60
rnd_01
Definition
exprtk_game_of_life.cpp:33
rnd_01::rnd_01
rnd_01()
Definition
exprtk_montecarlo_e.cpp:33
rnd_01::operator()
T operator()()
Definition
exprtk_montecarlo_e.cpp:36
Generated by
1.17.0