bugGNU Octave - Bugs: bug #67257, Comparison of complex numbers is...

 
 

bug #67257: Comparison of complex numbers is incompatible with Matlab

Submitter:  Dmitri A. Sergatskov <dasergatskov>
Submitted:  Sat 28 Jun 2025 04:41:04 PM UTC
   
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  None Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Release: 
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Wed 09 Jul 2025 12:30:17 PM UTC, comment #6: 

Yes, that language change would be accurate and possibly help understanding.

Rik <rik5>
Group administrator
Mon 07 Jul 2025 04:55:28 PM UTC, comment #5: 

If I'm understanding the conversation below correctly, would the warning

warning: comparing complex numbers is not supported in Matlab

be more accurately stated in current versions as:

warning: comparing the complex part of complex numbers is not supported in Matlab


Nicholas Jankowski <nrjank>
Group Member
Fri 04 Jul 2025 08:36:44 PM UTC, comment #4: 

Wow, Savannah is having some issues.  It appended a canned response 4 times at the end of the last post.

Rik <rik5>
Group administrator
Fri 04 Jul 2025 08:34:40 PM UTC, comment #3: 

I think the warning is accurate.  Matlab has supported comparing complex numbers for some time, but they do it by truncating the imaginary part and only using the real part.  This is inconsistent with other Matlab functions.  For max() and min(), Matlab establishes an ordering relationship for complex numbers based on magnitude (abs()) and if there is a tie then they base the comparison on phase angle.  Octave replicates this comparison scheme for max()/min() to be Matlab compatible.  It also extends this same ordering principle to gt() and lt().  The help text for max() has an illustrative example:


For complex arguments, the magnitude of the elements are used for
comparison.  If the magnitudes are identical, then the results are
ordered by phase angle in the range (-pi, pi].  Hence,

     max ([-1 i 1 -i])
         => -1

because all entries have magnitude 1, but -1 has the largest phase
angle with value pi.


But, there does seem to be a problem when comparing real to complex values.  What should happen is that both arguments are promoted to complex and then the ordering relationship above is used.  However, I can find a trivial example where that is not true.


-1 > i
ans = 0


On the other hand, if both arguments are complex then the result is correct.


complex (-1, 0) > complex (0, 1)
ans = 1


I think this bug report should be closed, but a new one opened for this incorrect behavior.


Thanks for taking the time to report this bug.

This bug report isn't very useful because it doesn't describe the
bug well. If you have time and can still reproduce the bug,
please read http://www.octave.org/bugs.html and add a more useful
description to this bug.


Thanks for taking the time to report this bug.

This bug report isn't very useful because it doesn't describe the
bug well. If you have time and can still reproduce the bug,
please read http://www.octave.org/bugs.html and add a description
of how to reproduce this bug.

You'll also need to add a stack trace; please see
http://wiki.octave.org/Debugging_Octave for more information
about how to do so.

Thanks in advance!


Thanks for taking the time to report this bug.

Unfortunately, that stack trace is missing some elements that
will help a lot to solve the problem, so it will be hard for the
developers to fix that crash. Can you get us a stack trace with
debugging symbols? Please see
http://wiki.octave.org/Debugging_Octave for more information on
how to do so. Thanks in advance!


Thanks for taking the time to report this bug.

However, you are using a version that is too old and not
supported anymore. Octave developers are no longer working on
that version, so unfortunately there will not be any bug fixes
for the version that you use.

By upgrading to a newer version of Octave you could receive bug
fixes and new functionality. Please feel free to reopen this bug
if the problem still occurs with a newer version of Octave.

Rik <rik5>
Group administrator
Fri 04 Jul 2025 04:31:43 PM UTC, comment #2: 

So the warning is incorrect (as of now). Matlab does support comparing complex numbers as per link I had provided.

May be it is something that has changed recently. In any case I will try to avoid this operation in my code.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Fri 04 Jul 2025 03:44:45 PM UTC, comment #1: 

Octave maintainer's have in the past specifically chose to implement a language extension above and beyond what Matlab does for complex numbers.


aoctave:1> warning ('on', 'Octave:language-extension')
octave:2> 1 < i
warning: comparing complex numbers is not supported in Matlab
ans = 1



Rik <rik5>
Group administrator
Sat 28 Jun 2025 04:41:04 PM UTC, original submission:  

Matlab's doc say
https://www.mathworks.com/help/matlab/ref/double.gt.html

Test Complex Numbers
Create a vector of complex numbers.

A = [1+i 2-2i 1+3i 1-2i 5-i];
Find the values that are greater than 2.

A(A > 2)
ans =
5.0000 - 1.0000i
gt compares only the real part of the elements in A.

Use abs to find which elements are outside a radius of 2 from the origin.

A(abs(A) > 2)
ans = 1×4 complex

   2.0000 - 2.0000i   1.0000 + 3.0000i   1.0000 - 2.0000i   5.0000 - 1.0000i

The result has more elements since abs accounts for the imaginary part of the numbers.


This is different from Octave:

octave:29> complex(-1,0) > 0
ans = 1
octave:30> A = [1+i 2-2i 1+3i 1-2i 5-i]
A =

   1 + 1i   2 - 2i   1 + 3i   1 - 2i   5 - 1i

octave:31> A(A > 2)
ans =

   2 - 2i   1 + 3i   1 - 2i   5 - 1i


I am not sure but I think the best fix is to do in data.cc:

 @seealso{le, eq, ge, gt, ne}
 @end deftypefn */)
 {
-  return binary_op_defun_body (octave_value::op_lt, args);
+  if (args.length () != 2)
+    print_usage ();
+
+  octave_value arg0 = args(0);
+  octave_value arg1 = args(1);
+
+  // For complex comparisons, compare only real parts
+  if (arg0.iscomplex () || arg1.iscomplex ())
+    return binary_op (octave_value::op_lt, arg0.real (), arg1.real ());
+
+  return binary_op (octave_value::op_lt, arg0, arg1);
 }


and similarly for "le", "eq", "gt", "ne".

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by dasergatskov (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.15-e6e5.
    Corresponding source code