C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_e_10kdigits.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Ten Thousand Digits Of E via Spigot Algorithm *
6 * Author: Arash Partow (1999-2024) *
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 <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T>
28{
29 typedef exprtk::symbol_table<T> symbol_table_t;
30 typedef exprtk::expression<T> expression_t;
31 typedef exprtk::parser<T> parser_t;
32
33 symbol_table_t symbol_table;
34
35 symbol_table.add_function("print",
36 [](T t) -> T
37 {
38 printf("%1d",static_cast<int>(t));
39 return T(1);
40 });
41
42 const std::string e_10k_digits_spigot =
43 " const var num_digits := 10000; "
44 " var a[num_digits] := [2:1]; "
45 " var b[num_digits] := [1]; "
46 " "
47 " print(2); "
48 " "
49 " for (var digit := 0; digit < num_digits; digit += 1) "
50 " { "
51 " b *= 10; "
52 " "
53 " for (var i := (b[] - 1); i > 0; i -= 1) "
54 " { "
55 " var q := floor(b[i] / a[i]); "
56 " var r := floor(b[i] % a[i]); "
57 " b[i - 1] := b[i - 1] + q; "
58 " b[i] := r; "
59 " }; "
60 " "
61 " print(floor(b[0] / a[0])); "
62 " "
63 " b[0] %= a[0]; "
64 " } ";
65
66 expression_t expression;
67 expression.register_symbol_table(symbol_table);
68
69 parser_t parser;
70 parser.compile(e_10k_digits_spigot,expression);
71
72 expression.value();
73}
74
75int main()
76{
77 e_10k_digits<double>();
78 return 0;
79}
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
void e_10k_digits()
int main()