why matlab returns Inf value

Status
Not open for further replies.
The MATLAB software creates the ans variable automatically when you specify no output argument.
When you type "V", Matlab gives its value but the ans variable is not updated. It preserves its previous value, that must have been big enough that 2^ans overflows.
Regards

Z
 

zorro is correct. The value you assign to V is not automatically assigned to the "ans" variable. You are seeing Inf because the value of "ans" is a large number left over from something you were doing before. You can check the value of ans by just typing "ans".

Try the following code instead:
Code:
format long g;
V = 39.139000142183406;
W = 2^V

W =

          605359055128.849
This assigns the result to the variable "W", so ans is not affected.

Alternatively, if we write:
Code:
format long g;
V = 39.139000142183406;
2^V

ans =

          605359055128.849
then "ans" is assigned because we didn't explicitly state a variable to assign to.
 
no this was just for example, I get Inf while running a program.
As pointed out, the example is meaningless. We still don't know which of your calculations gets inf result.

Unless otherwise specified, Matlab uses double precision format. So 2^n will overflow to inf for n > 1023.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…