34 typedef typename compositor_t::function function_t;
38 symbol_table_t symbol_table;
40 symbol_table.add_constants();
41 symbol_table.add_variable(
"x",x);
43 compositor_t compositor(symbol_table);
46 function_t(
"newton_sqrt")
52 " case x < 0 : null; "
58 " var sqrt_x := x / 2; "
60 " if (equal(sqrt_x^2, x)) "
63 " sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); "
64 " until ((z -= 1) <= 0); "
69 const std::string expression_str =
"newton_sqrt(x)";
71 expression_t expression;
72 expression.register_symbol_table(symbol_table);
75 parser.compile(expression_str,expression);
77 for (std::size_t i = 0; i < 1000; ++i)
79 x =
static_cast<T
>(i);
81 const T result = expression.value();
82 const T
real = std::sqrt(x);
83 const T error = std::abs(result -
real);
85 printf(
"sqrt(%03d) - Result: %15.13f\tReal: %15.13f\tError: %18.16f\n",
86 static_cast<unsigned int>(i),