[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.2 Numerical evaluation
GiNaC keeps algebraic expressions, numbers and constants in their exact form. To evaluate them using floating-point arithmetic you need to call
ex ex::evalf(int level = 0) const; |
The accuracy of the evaluation is controlled by the global object Digits
which can be assigned an integer value. The default value of Digits
is 17. See section Numbers, for more information and examples.
To evaluate an expression to a double
floating-point number you can
call evalf()
followed by numeric::to_double()
, like this:
{ // Approximate sin(x/Pi) symbol x("x"); ex e = series(sin(x/Pi), x == 0, 6); // Evaluate numerically at x=0.1 ex f = evalf(e.subs(x == 0.1)); // ex_to<numeric> is an unsafe cast, so check the type first if (is_a<numeric>(f)) { double d = ex_to<numeric>(f).to_double(); cout << d << endl; // -> 0.0318256 } else // error } |