octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add an operator<(Complex, Complex) to oct-sort.cc for non-g++ co


From: Jason Riedy
Subject: [PATCH] Add an operator<(Complex, Complex) to oct-sort.cc for non-g++ compilers.
Date: Thu, 29 Nov 2007 15:49:11 -0800
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/23.0.50 (gnu/linux)

The standard rightly does not define ordering operators, but g++'s
libstdc++ provides them.  Other compilers (e.g. icc) do not, and
oct-sort.cc refuses to compile.  The included operator is never
actually *used* in Octave; src/DLD-FUNCTIONS/sort.cc defines
comparison functions for Complex sorts.

Signed-off-by: Jason Riedy <address@hidden>
---
    I err on the side of too many comments.  And is it ok to
    use gmane for posting?  It's easier for me than managing
    yet more list subscriptions.

 liboctave/ChangeLog   |    6 ++++++
 liboctave/oct-sort.cc |   19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog
index bec3941..a357aeb 100644
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,9 @@
+2007-11-29  Jason Riedy  <address@hidden>
+
+       * oct-sort.c (operator < (Complex, Complex)): Standard C++ does
+       not define ordering operators for complex numbers.  The IFT
+       macro requires an operator < to compile, so define one.
+
 2007-11-26  John W. Eaton  <address@hidden>
 
        * idx-vector.h (idx_vector::idx_vector_rep (const intNDArray<U>&)):
diff --git a/liboctave/oct-sort.cc b/liboctave/oct-sort.cc
index 84f0054..56cf8d3 100644
--- a/liboctave/oct-sort.cc
+++ b/liboctave/oct-sort.cc
@@ -92,6 +92,25 @@ The Python license is
 
 #define IFLT(a,b)  if (compare == NULL ? ((a) < (b)) : compare ((a), (b)))
 
+/*
+  There is no natural total ordering for complex numbers, and
+  the C++ standard rightly does not specify one.  The following
+  definition matches MATLAB(tm)'s specified sort order and allows
+  IFLT(a,b) to compile even though operator< is never used by
+  Octave's Complex sort (see ../src/DLD-FUNCTIONS/sort.cc).
+*/
+static inline bool
+operator < (const Complex a, const Complex b)
+{
+    using namespace std;
+    const double na = norm (a), nb = norm (b);
+    if (na < nb)
+       return true;
+    if (na == nb && arg (a) < arg (b))
+       return true;
+    return false;
+}
+
 template <class T>
 octave_sort<T>::octave_sort (void) : compare (NULL) 
 { 
-- 
1.5.3.6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]