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; "
57 " var remaining_itrs := 100; "
58 " var sqrt_x := x / 2; "
60 " if (equal(sqrt_x * sqrt_x, x)) "
63 " sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); "
64 " until ((remaining_itrs -= 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 (x = T(0); x < T(500); x += T(0.5))
79 const T result = expression.value();
80 const T
real = std::sqrt(x);
81 const T error = std::abs(result -
real);
85 printf(
"sqrt(%6.2f) - Result: %15.13f\tReal: %15.13f\tError: %18.16f EIB: %c\n",
90 err_in_bound ?
'T' :
'F');