[GiNaC-devel] [PATCH] Fix comparison ordering in relational.

ovf ovf at mm.st
Mon Dec 20 22:33:34 CET 2021


Reported by Feng Feng <f.feng at outlook.com> in https://www.ginac.de/pipermail/ginac-list/2021-December/002368.html
---
 check/exam_relational.cpp | 17 +++++++++++++++++
 ginac/relational.cpp      |  4 ++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/check/exam_relational.cpp b/check/exam_relational.cpp
index 3b652b05..f66911f3 100644
--- a/check/exam_relational.cpp
+++ b/check/exam_relational.cpp
@@ -94,6 +94,21 @@ static unsigned exam_relational_possymbol()
 	return result;
 }
 
+// Comparisons should maintain ordering invariants
+static unsigned exam_relational_order()
+{
+	unsigned result = 0;
+	numeric i = 1ll<<32, j = i+1;
+	symbol a;
+	relational x = i==a, y = j==a;
+	if (x.compare(y) != -y.compare(x)) {
+		clog << "comparison should be antisymmetric." << endl;
+		result += 1;
+	}
+
+	return result;
+}
+
 // Very simple arithmetic should be supported, too.
 static unsigned exam_relational_arith()
 {
@@ -125,10 +140,12 @@ unsigned exam_relational()
 	result += exam_relational_elementary(); cout << '.' << flush;
 	result += exam_relational_possymbol(); cout << '.' << flush;
 	result += exam_relational_arith(); cout << '.' << flush;
+	result += exam_relational_order(); cout << '.' << flush;
 
 	return result;
 }
 
+
 int main(int argc, char** argv)
 {
 	return exam_relational();
diff --git a/ginac/relational.cpp b/ginac/relational.cpp
index 599a2635..dbc541a3 100644
--- a/ginac/relational.cpp
+++ b/ginac/relational.cpp
@@ -223,8 +223,8 @@ int relational::compare_same_type(const basic & other) const
 				return (o < oth.o) ? -1 : 1;
 			break;
 	}
-	const int lcmpval = lh.compare(oth.rh);
-	return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
+	const int lcmpval = lh.compare(oth.lh);
+	return (lcmpval!=0) ? lcmpval : rh.compare(oth.rh);
 }
 
 bool relational::match_same_type(const basic & other) const
-- 
2.34.1



More information about the GiNaC-devel mailing list