matlab giving complex number when the expected answer is a real number?
2 次查看(过去 30 天)
显示 更早的评论
matlab is giving me complex number when the expected answer is a real number? The code is the following:
A1 = -1;
A = A1^1.5
A = 0.0000 - 1.0000i
The answer should have been -1
A = -1^1.5
A = -1
Can anyone tell me why this is the case? It is giving a complex number when a variable is used.
Many thanks
1 个评论
Stephen23
2018-5-24
"Can anyone tell me why this is the case?"
Because unary negation has a lower priority than the power operation. You can check this yourself:
采纳的回答
Mischa Kim
2018-5-24
Note:
>> (-1)^1.5
ans =
0.0000 - 1.0000i
which is not the same as
>> -1^1.5 % = -(1^1.5)
ans =
-1
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!