Thread Subject: 100^100 != 100^100

Subject: 100^100 != 100^100

From: ustunozgur

Date: 31 Oct, 2009 12:05:16

Message: 1 of 3

 a = 100^100; str2num(num2str(a)) == a

results in 1

but

num2str(a) =
99999999999999996973312221251036165947450327545502362648241750950346848435554075
53419633840470625186802751241597388240818213573436827848463938504104723987787102
3591066789981811181813306167128854888448


Howcome? What is the best way to deal with such large numbers in
MATLAB? How can I see the number without num2str function?

Best regards,

Ustun

Subject: 100^100 != 100^100

From: Nasser M. Abbasi

Date: 31 Oct, 2009 12:21:50

Message: 2 of 3


"ustunozgur" <ustunozgur@gmail.com> wrote in message
news:7e2e0d2d-b779-4137-92f5-
>
> Howcome? What is the best way to deal with such large numbers in
> MATLAB? How can I see the number without num2str function?
>
> Best regards,
>
> Ustun

either use symbolic as in

a=sym(99999999999999996973312221251036165947450327545502362648241750950346848435554075534196338404706251868027512415973882408182135734368278484639385041047239877871023591066789981811181813306167128854888448)

(but then you are stuck with the symbolic domain and can't use other Matlab
numeric functions, or use large numbers package as "Variable Precision
Integer Arithmetic" in file exchange, or work with the logs of the numbers
instead of the numbers themselves.

--Nasser

Subject: 100^100 != 100^100

From: Tim Davis

Date: 31 Oct, 2009 13:05:04

Message: 3 of 3

ustunozgur <ustunozgur@gmail.com> wrote in message <7e2e0d2d-b779-4137-92f5-8b4b2807764f@g23g2000yqh.googlegroups.com>...
> a = 100^100; str2num(num2str(a)) == a
>
> results in 1
>
> but
>
> num2str(a) =
> 99999999999999996973312221251036165947450327545502362648241750950346848435554075
> 53419633840470625186802751241597388240818213573436827848463938504104723987787102
> 3591066789981811181813306167128854888448
>
>
> Howcome? What is the best way to deal with such large numbers in
> MATLAB? How can I see the number without num2str function?
>

You're dealing with floating-point roundoff. The result you're getting from
num2str is O(eps) different than 100^100, in terms of relative error.
If you need a 200+ digit number, with all digits represented, don't use floating-point.
Use VPA.

  x = vpa ('100^100', 210)

Alternatively, use fprintf

fprintf ('%32.18g\n', 100^100);

which reports 1e+200.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com