# Inf and NaN problem

3 ビュー (過去 30 日間)
Hajar 2015 年 1 月 14 日
コメント済み: Star Strider 2015 年 1 月 14 日
I have to do some computations with very big numbers values, so my matlab code returns "Inf" and "NaN.. I want to know if there is a way to avoid this problem? here is the part of my code that returns Inf and NaN
UtiliteProba(b,l)=exp((UtiliteB(b,l)+UtiliteC(b,l))/T);
proba(b,l)=UtiliteProba(b,l)/constante(1,b);
Actually the "UtiliteProba"= Inf and "proba"=NaN , the "constante" is equal to Inf too..
thank you so much for your help

サインインしてコメントする。

### 回答 (2 件)

Iain 2015 年 1 月 14 日
Logarithms are your friends. Here's what I mean maths:
log(UtiliteProba) = (UtiliteB(b,l)+UtiliteC(b,l))/T (values in the range of a few hundred correspond to the maximum values matlab can handle with doubles)
proba = e^(log(UtiliteProba) - log(constante(1,b))
log(proba) = (log(UtiliteProba) - log(constante(1,b))
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Hajar 2015 年 1 月 14 日
I forgot to mention that
constante(1,b)=sum(UtiliteProba(b,:))
then
log(proba)=log(utiliteProba)-log(constante)
=log(utiliteProba)-log(sum(utiliteProba))
for log(utiliteProba) it's okay since I can calculate it by using (UtiliteB(b,l)+UtiliteC(b,l))/T , but log(constante) would be log(inf) :s :s

サインインしてコメントする。

Star Strider 2015 年 1 月 14 日
The only way I can imagine to avoid the Inf and NaN values (assuming none of the arguments ‘UtiliteB’ and ‘UtiliteC’ are either Inf or NaN) is to not take the exponential and keep them as logarithms until you need to actually evaluate them as exponentials:
UtiliteProba(b,l) = ((UtiliteB(b,l)+UtiliteC(b,l))/T);
proba(b,l) = UtiliteProba(b,l) - log(constante(1,b));
##### 2 件のコメントなしを表示なしを非表示
Hajar 2015 年 1 月 14 日
actually constante(1,b)=sum(UtiliteProba(b,l)) :s
Star Strider 2015 年 1 月 14 日
â€˜log(constante) would be log(inf)â€™
If â€˜constante = Infâ€™, then none of your calculations using it will produce any useful results.
You need to review your calculation of â€˜constanteâ€™ and see what the problem is with it.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by