# bug #51556: function isreal applied on complex numbers with zero imaginary part

 Submitted by: Ernst Reissner Submitted on: Mon 24 Jul 2017 12:02:48 PM UTC Category: Octave Function Severity: 3 - Normal Priority: 5 - Normal Item Group: Incorrect Result Status: None Assigned to: None Originator Name: Open/Closed: Closed Release: dev Operating System: Any

You are not logged in

## Discussion

Mon 24 Jul 2017 02:23:01 PM UTC, comment #4:

In Matlab 2016a, sorry not the latest, "isreal (0i)" returns true. The sequence from comment #2 returns the results in comments. So I guess this report can be closed as invalid.

Pantxo Diribarne <pantxo>
Mon 24 Jul 2017 02:04:33 PM UTC, comment #3:

To confirm JWE's presumption, Matlab r2017b prerelease gives:

>> isreal (0i)
ans =
logical
1
>>

and r2016b does exactly the same.

Furthermore,

>> format compact
>> x = complex (12, 0)  %% 12+0i
isreal (x)           %% false
y = 1 + x            %% 13
isreal (y)           %% true
x =
12.0000 + 0.0000i
ans =
logical
0
y =
13
ans =
logical
1
>>

Philip Nienhuis <philipnienhuis>
Mon 24 Jul 2017 01:26:20 PM UTC, comment #2:

In Matlab, (and Octave, for compatibility reasons) if you want to create a complex value with a zero imaginary part, you need to use something like "complex (r, 0)".  And even then, if you use that value in an expression like this:

x = complex (12, 0)  %% 12+0i
isreal (x)           %% false
y = 1 + x            %% 13
isreal (y)           %% true

you will get a real value as the final result because of the automatic complex -> real conversion rules for (nearly all) expression results.

I'm certainly not claiming this is mathematically correct or necessarily the best programming language design.  But it is the way Matlab works and we have to do the same for compatibility reasons.  If we didn't, we'd be constantly getting bug reports that Octave fails on code that works perfectly well in Matlab.

John W. Eaton <jwe>
Mon 24 Jul 2017 01:16:41 PM UTC, comment #1:

I'm pretty sure that Matlab automatically narrows the result of the expression 0i to be real, so that isreal (0i) actually returns true.

Could someone please verify with the latest Matlab?

John W. Eaton <jwe>
Mon 24 Jul 2017 12:02:48 PM UTC, original submission:

isreal(x) determines whether a has real type.
Consequently, isreal(0i) is false in matlab.
also isreal([0i 1]) is just false, a single scalar value
because the type is meant, not zero imaginary part in all components.

The current usage of isreal(A)
which means imag(A) is nonzero may be substituted by

~any(imag(A)).

Ernst Reissner <ernstreissner>

## Attached Files

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

Attach Files:

Comment:

No files currently attached

## Dependencies

Depends on the following items: None found

Items that depend on this one: None found

Carbon-Copy List
• -email is unavailable- added by rik5 (Updated the item)
• -email is unavailable- added by pantxo (Posted a comment)
• -email is unavailable- added by jwe (Posted a comment)
• -email is unavailable- added by ernstreissner (Submitted the item)
• -email is unavailable- added by ernstreissner
•

Do you think this task is very important?
This task has 0 encouragements so far.

Only project members can vote.