Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problems with log being infinity when it should not be

Subject: problems with log being infinity when it should not be

From: Irene

Date: 28 May, 2010 21:33:22

Message: 1 of 5

Hello,
I am not sure how to correct this problem in Matlab.

When I calculate:
log(factorial(100)/(10000^100))
I get
-Inf

However, when I calculate:

factorial(100) = 9.33*10^157
log(9.33*10^157/10^400)
I get
-557.2949

Mathematica also obtains the latter answer.
How should I tell matlab that I want the last answer and not -Inf?

Thank you!

Subject: problems with log being infinity when it should not be

From: Walter Roberson

Date: 28 May, 2010 21:53:25

Message: 2 of 5

Irene wrote:

> I am not sure how to correct this problem in Matlab.
>
> When I calculate:
> log(factorial(100)/(10000^100))
> I get -Inf
>
> However, when I calculate:
>
> factorial(100) = 9.33*10^157
> log(9.33*10^157/10^400)
> I get
> -557.2949
>
> Mathematica also obtains the latter answer.
> How should I tell matlab that I want the last answer and not -Inf?

You cannot. 10^400 is not representable in Matlab. The parser must be doing
something special to be able to use it in a constant expression.

In 2008b, the constant expression returns -Inf as well.

If you have the symbolic toolbox, you could evaluate the expression in that,
as that has a much higher range, in the thousands of digits.

Subject: problems with log being infinity when it should not be

From: James Tursa

Date: 28 May, 2010 22:01:24

Message: 3 of 5

"Irene " <jez_zaz_bel@yahoo.com> wrote in message <htpcr2$4av$1@fred.mathworks.com>...
> Hello,
> I am not sure how to correct this problem in Matlab.
>
> When I calculate:
> log(factorial(100)/(10000^100))
> I get
> -Inf
>
> However, when I calculate:
>
> factorial(100) = 9.33*10^157
> log(9.33*10^157/10^400)
> I get
> -557.2949
>
> Mathematica also obtains the latter answer.
> How should I tell matlab that I want the last answer and not -Inf?
>
> Thank you!

Pay attention to your intermediate calculations. e.g.,

>> 10000^100
ans =
   Inf
>> 10^400
ans =
   Inf

You will either need to use the symbolic toolbox, or work with sums of logs instead of the values directly, or do some of the exponent calculations by hand before you give it to MATLAB, or something else to avoid the limitations of IEEE Double Precision format.

James Tursa

Subject: problems with log being infinity when it should not be

From: Roger Stafford

Date: 28 May, 2010 23:15:22

Message: 4 of 5

"Irene " <jez_zaz_bel@yahoo.com> wrote in message <htpcr2$4av$1@fred.mathworks.com>...
> Hello,
> I am not sure how to correct this problem in Matlab.
>
> When I calculate:
> log(factorial(100)/(10000^100))
> I get
> -Inf
>
> However, when I calculate:
>
> factorial(100) = 9.33*10^157
> log(9.33*10^157/10^400)
> I get
> -557.2949
>
> Mathematica also obtains the latter answer.
> How should I tell matlab that I want the last answer and not -Inf?
>
> Thank you!

  It is not reasonable to use matlab's double precision floating point numbers to do such a calculation as that in the form you have used. These numbers cannot go beyond about 1.8e+308 before becoming "infinity". You may be sure Mathematica does not use them for this computation as it stands. As both Walter and James have said, use matlab's symbolic toolbox for a fair comparison with Mathematica's results for anything using such large numbers.

  James has given you an alternative of using sums of logarithms and converting exponents to products. Write it like this:

 sum(log(1:100))-100*log(10000)

Roger Stafford

Subject: problems with log being infinity when it should not be

From: Irene

Date: 29 May, 2010 15:54:05

Message: 5 of 5

"Irene " <jez_zaz_bel@yahoo.com> wrote in message <htpcr2$4av$1@fred.mathworks.com>...
> Hello,
> I am not sure how to correct this problem in Matlab.
>
> When I calculate:
> log(factorial(100)/(10000^100))
> I get
> -Inf
>
> However, when I calculate:
>
> factorial(100) = 9.33*10^157
> log(9.33*10^157/10^400)
> I get
> -557.2949
>
> Mathematica also obtains the latter answer.
> How should I tell matlab that I want the last answer and not -Inf?
>
> Thank you!

I see. Thank you!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us