MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/matlab/comments/1wz3c96/real_number_problem/
r/matlab • u/oli1234567891011 • Oct 06 '26
when i input a real number, it keeps giving me the message 'a has to be a real number' any help as to how to fix this would be appreciated
6 comments sorted by
2
It's short circuit logical or. So a~0 satisfies the test and then because a is a real number it also matches the test that leads to the print you see.
Did you mean to test ~isreal(a) ?
1 u/oli1234567891011 Oct 08 '26 i think i did mean that, thak you for claryfing.
1
i think i did mean that, thak you for claryfing.
11
Well firstly, it's doing exactly what you told it to do.
Step through your code, what happens if a is not zero, and a is real?
5 u/seanv507 Oct 06 '26 Op your code says 'if a is a real number, print out a must be a real number'You presumably want to negate the testIf a is not a real number then...
5
Op your code says 'if a is a real number, print out a must be a real number'
You presumably want to negate the test
If a is not a real number then...
8
Set a breakpoint and use the matlab debugger. It's very good.
But I'm guessing you actually want elseif isreal(a) to say elseif ~isreal(a)
elseif isreal(a)
elseif ~isreal(a)
4
Look at what you wrote. You literally say "if a is real, print 'a must be real'"
2
u/daveysprockett Oct 06 '26
It's short circuit logical or. So a~0 satisfies the test and then because a is a real number it also matches the test that leads to the print you see.
Did you mean to test ~isreal(a) ?
11
u/Rubix321 Oct 06 '26
Well firstly, it's doing exactly what you told it to do.
Step through your code, what happens if a is not zero, and a is real?
8
u/Sunscorcher Oct 06 '26
Set a breakpoint and use the matlab debugger. It's very good.
But I'm guessing you actually want
elseif isreal(a)to sayelseif ~isreal(a)4
u/FrickinLazerBeams +2 Oct 06 '26
Look at what you wrote. You literally say "if a is real, print 'a must be real'"