C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_pi_10kdigits.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Ten Thousand Digits Of Pi 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 pi_10k_digits_spigot =
43 " const var num_digits := 10000; "
44 " const var offset := 14; "
45 " var a[3.5 * num_digits + offset]; "
46 " var size := a[] - offset; "
47 " var d := 0; "
48 " var c := 0; "
49 " var h := 0; "
50 " var f := size / 3.5; "
51 " "
52 " for (var x := 0; x < size ; x += offset) "
53 " { "
54 " var b := size - x - 1; "
55 " var g := 2 * b; "
56 " "
57 " d %= f; "
58 " c := d; "
59 " "
60 " while (g > 0) "
61 " { "
62 " var k := floor(b); "
63 " var i := (h > 0) ? a[k] : floor(f / 5); "
64 " d := (d * b) + (f * i); "
65 " g -= 1; "
66 " b -= 1; "
67 " a[k] := d % g; "
68 " d := floor(d / g); "
69 " g := 2 * b; "
70 " }; "
71 " "
72 " var v := c + floor((1 * d) / f); "
73 " "
74 " for (var j := 1000; j >= 1; j := j / 10) "
75 " { "
76 " print(floor(v / j)); "
77 " v %= j; "
78 " }; "
79 " "
80 " h := 1; "
81 " } ";
82
83 expression_t expression;
84 expression.register_symbol_table(symbol_table);
85
86 parser_t parser;
87 parser.compile(pi_10k_digits_spigot,expression);
88
89 expression.value();
90}
91
92int main()
93{
94 pi_10k_digits<double>();
95 return 0;
96}
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
void pi_10k_digits()
int main()