[GiNaC-list] Re: Behaviour of evalf()

Martin Sandve Alnæs martinal at simula.no
Thu Nov 15 10:14:28 CET 2007


2007/11/14, Alexei Sheplyakov <varg at theor.jinr.ru>:
> On Wed, Nov 14, 2007 at 04:41:03PM +0100, Martin Sandve Alnæs wrote:
>
> > Is it possible to make evalf() let integer constants stay integers?
>
> evalf() turns exact numbers into inexact _by definition_. Keeping the
> coefficient exact (for instance, integer) does not make sense anyway,
> since they are multiplied by inexact number. If you still insist on it,
> you could roll down your own evalf-alike function, e.g.
>
> struct weird_evalf() : public map_function {
>         ex operator()(const ex& e) {
>                 if (is_a<add>(e) || is_a<mul>(e))
>                         return e.map(*this);
>                 if (is_a<power>(e))
>                         return power(e.op(0).map(*this), e.op(1));
>                 if (is_a<numeric>(e))
>                         return e;
>                 else
>                         return e.evalf();
> }

Ok, thanks. The reason I asked was that I encountered a situation I
can't reproduce right now, where terms like "x^3" were converted to
"x^3.0" and collect wouldn't work on them afterwards since the
exponents were non-integers.

Thinking about it now, it's probably a bug in my own code, I might
call evalf on "3" in "x^3" someplace. Because (x^3).evalf() returns
(x^3), so it doesn't seem to be ginacs behaviour.

--
Martin


More information about the GiNaC-list mailing list