Expression comparison
Hallo, I am a GiNaC newbe and I seem not to understand expression comparisons fully, 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); } Where Num is GiNaC::ex. Doing v0.evalf() < v1.evalf() works. Can somebody give me (or point me to) some info about expression comparison? Cheers and thx in advance, Peter
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
Hi, thanks again for the quick reply, I looked at the things closely and try to summarize them for me: 1) it is OK to use '==' and '!=' operators with expressions. 2) be careful with < <= > >= since they are for internal usage and do not give you the comparison of the value of the expression. Correct? Cheers, Peter 2008/9/29, Jens Vollinga <jensv@nikhef.nl>:
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
_______________________________________________ GiNaC-devel mailing list GiNaC-devel@ginac.de https://www.cebix.net/mailman/listinfo/ginac-devel
Hi, Peter Grobarcik schrieb:
1) it is OK to use '==' and '!=' operators with expressions.
right.
2) be careful with < <= > >= since they are for internal usage and do not give you the comparison of the value of the expression.
Well, yes and no. They are also for external use if you need to sort your expressions (for example when putting them in a STL::set). But if you want to compare the value of an expression then you have to make sure the expression is a number (evalf is your friend). Regards, Jens
participants (2)
-
Jens Vollinga
-
Peter Grobarcik