NAN+inf

in my paper i need to calculate exp()...here in my code i got exp(4096) but due to this i am getting NAN+inf in all larger value areas..what to do for this?whether tis indicates any error or this NAN+inf for some values are acceptable?

 Accepted Answer

Walter Roberson
Walter Roberson on 8 Sep 2011

0 votes

Which MATLAB version are you using?
Please use size() and iscomplex() on the result variable to see if it is perhaps a complex number.
If you are contemplating using values that large, you should check to see if you can find a way to rewrite the calculation using the log of the values instead. That will not always be possible but when values that large are being discussed, it is not uncommon for there to be a log transformation of the problem.
exp(4096) is, by the way, approximately 7.416480782e1778
If you must use such large values and it is not possible to do a log transformation, then you probably need to use the symbolic toolbox, possibly with a large Digits setting.

10 Comments

x
x on 8 Sep 2011
sry sir..how to use calculation using log of that values instead of exp....
exp(4096) is, by the way, approximately 7.416480782e1778
hoe this is obtained sir inorder to avoid NAN+inf
Walter Roberson
Walter Roberson on 8 Sep 2011
log( sqrt(det(covariance matrix))*exp(- covariance value) )
is log(det(covariance matrix))/2 - covariance value
It would not be uncommon in equations for it to be possible to work with or even compare these logs rather than the full value. But of course you do need to watch out for complex numbers being generated such as if the determinant is negative.
Walter Roberson
Walter Roberson on 8 Sep 2011
I evaluated exp(4096) in Maple, but you could calculate it readily in MATLAB
log10(exp(4096)) = log(exp(4096))/log(10) = 4096 / log(10) ~= 1778.870198 . The 1778 gives you the power of 10, and 10 raised to the fraction gives you the mantissa.
x
x on 8 Sep 2011
sry sir can't get it .. exp(4096) is inf in matlab...then log(exp(4096)) is also inf...then how this value is approximately 7.416480782e1778
x
x on 8 Sep 2011
in matlab log10(exp(4096)),log(exp(4096))/log(10) also returns inf only then how?
Walter Roberson
Walter Roberson on 8 Sep 2011
Use logic rather than brute force.
function s = large_exp(x)
if x == 0
s = '1';
else
t = x / log(10);
s = sprintf('%fe%d', 10^(t - floor(t)), floor(t));
end
end
Walter Roberson
Walter Roberson on 8 Sep 2011
Notice that this returns a string, since the value will often be outside of the range that is representable in a double precision number. The routine could also be coded to return a vector of the mantissa and exponent:
function expx = large_exp(x)
t = x/log(10);
expx = [10^(t-floor(t)), floor(t)];
end
Andrei Bobrov
Andrei Bobrov on 8 Sep 2011
exp(4096) -> 10^x
x = 4096/ln(10)
x ~= 1778.870198
M = 10^rem(x,1) ~= 7.41648
p = fix(x) = 1778
exp(4096) -> 7.41648e1778
use M (mantissa)
x
x on 8 Sep 2011
plz .......help me sir......manually i found how this answer returns..but in matlab the value is inf only ..how to do it in matlab
Walter Roberson
Walter Roberson on 8 Sep 2011
Use my large_exp() function instead of exp(), but then make appropriate adjustments for the fact that large_exp() returns a pair of numbers, the first being the mantissa, and the second being the power of 10.
But I still think it is more likely that you can work with the log() of the equations, never taking the exp() of 4096.

Sign in to comment.

More Answers (1)

Paulo Silva
Paulo Silva on 8 Sep 2011

0 votes

Can you please tell my why are you trying to get a result from exp(4096)? you do know that the result would be a very huge number, MATLAB can't find it.

4 Comments

x
x on 8 Sep 2011
in my project i have one equation sqrt(det(covariance matrix))*exp(- covariance value) for various patch i wnat to calculate this..at some patches this covariance value is negative so exp(some positive big value) gives NAN+inf..if u cant undrstand can i mail u the base paper
Walter Roberson
Walter Roberson on 8 Sep 2011
If the det() of the covariance matrix is negative, then the sqrt() of it will be complex, which you then multiply by infinity. In particular if the real part of the sqrt() came out as 0 but the complex part came out positive, you would have 0*infinity + positive_number*infinity*sqrt(-1) . 0*infinity is NaN, but the second part would come out as +infinity*sqrt(-1), for a net result represented as NaN +infinity*i
x
x on 8 Sep 2011
sqrt is complex at some cases..al the above conditions which you mentioned exists in my calculation sir..some of determinant value is negative tooo.how to solve this problem sir?
Walter Roberson
Walter Roberson on 8 Sep 2011
What would you _like_ the result to come out as if the determinant turns out to be negative ?

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!