mirror of
https://github.com/ArashPartow/exprtk.git
synced 2025-06-22 16:28:57 +00:00
C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
* C++ Mathematical Expression Toolkit Library *
|
||||
* *
|
||||
* Simple Example 10 *
|
||||
* Author: Arash Partow (1999-2018) *
|
||||
* Author: Arash Partow (1999-2020) *
|
||||
* URL: http://www.partow.net/programming/exprtk/index.html *
|
||||
* *
|
||||
* Copyright notice: *
|
||||
@ -19,6 +19,7 @@
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
#include "exprtk.hpp"
|
||||
|
||||
|
||||
@ -44,25 +45,26 @@ void newton_sqrt()
|
||||
.add(
|
||||
function_t( // define function: newton_sqrt(x)
|
||||
"newton_sqrt",
|
||||
" switch "
|
||||
" { "
|
||||
" case x < 0 : -inf; "
|
||||
" case x == 0 : 0; "
|
||||
" case x == 1 : 1; "
|
||||
" default: "
|
||||
" ~{ "
|
||||
" var z := 100; "
|
||||
" var sqrt_x := x / 2; "
|
||||
" repeat "
|
||||
" sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); "
|
||||
" if (equal(sqrt_x^2, x)) "
|
||||
" break[sqrt_x]; "
|
||||
" until ((z -= 1) <= 0); "
|
||||
" }; "
|
||||
" } ",
|
||||
" switch "
|
||||
" { "
|
||||
" case x < 0 : null; "
|
||||
" case x == 0 : 0; "
|
||||
" case x == 1 : 1; "
|
||||
" default: "
|
||||
" ~{ "
|
||||
" var z := 100; "
|
||||
" var sqrt_x := x / 2; "
|
||||
" repeat "
|
||||
" if (equal(sqrt_x^2, x)) "
|
||||
" break[sqrt_x]; "
|
||||
" else "
|
||||
" sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); "
|
||||
" until ((z -= 1) <= 0); "
|
||||
" }; "
|
||||
" } ",
|
||||
"x"));
|
||||
|
||||
std::string expression_str = "newton_sqrt(x)";
|
||||
const std::string expression_str = "newton_sqrt(x)";
|
||||
|
||||
expression_t expression;
|
||||
expression.register_symbol_table(symbol_table);
|
||||
@ -74,9 +76,9 @@ void newton_sqrt()
|
||||
{
|
||||
x = static_cast<T>(i);
|
||||
|
||||
T result = expression.value();
|
||||
const T result = expression.value();
|
||||
|
||||
printf("sqrt(%03d) - Result: %12.10f\tReal: %12.10f\n",
|
||||
printf("sqrt(%03d) - Result: %15.13f\tReal: %15.13f\n",
|
||||
static_cast<unsigned int>(i),
|
||||
result,
|
||||
std::sqrt(x));
|
||||
|
Reference in New Issue
Block a user