[GiNaC-devel] Expression comparison

Jens Vollinga jensv at nikhef.nl
Mon Sep 29 13:13:44 CEST 2008


Hi,

Peter Grobarcik schrieb:
> I have following test case which does not work as I expected:
> 
>     void testNumComparison() {
>     	Num v0 = sqrt(Num(2));
>     	Num v1 = sqrt(Num(10));
>     	stringstream strm;
>     	strm << "v0("<<v0<<") < v1("<<v1<<") ?";
>     	CPPUNIT_ASSERT_MESSAGE(strm.str(),v0<v1);
>    }

> Can somebody give me (or point me to) some info about expression comparison?

First, sqrt(Num(20)) is internally still a symbolic expression. Only if 
you write sqrt(Num(20.0)) or evalf(sqrt(Num(20)) the expression will 
consist of a numeric object that can be tested with <,>,!=,== in the 
expected way. GiNaC doesn't compute sqrt(20) to its value 4.47... as 
long as the numbers involved (20) are exact and are not of floating 
point type. evalf, of course, forces the evaluation.

Second, ==,!= work (on symbolic expressions) very different from <,>.
== and != can be evaluated immediately and this is what GiNaC does. 
Effectively, GiNaC will return true or false. <,> on the other hand test 
for the internal ordering of the expressions and determine the canonical 
ordering. This ordering is mainly for internal purposes of GiNaC and has 
nothing to do with the would-be value of the expression.

Regards,
Jens




More information about the GiNaC-devel mailing list