int2str returns the wrong result

>> int2str(73167176531330624919225119674426574742355349194934)
ans =
73167176531330625585928818171611107149744948903936
Anyone know what's going on here? I'm guessing the number is too because when I evaluate the difference between the input and output, the answer is 0.
What causes Matlab to fill in nonsense numbers though?

1 comentario

Stephen23
Stephen23 el 8 de Jul. de 2015
Editada: Stephen23 el 8 de Jul. de 2015
double numbers, the default in MATLAB and many other programming languages, only support between 15 and 17 significant figures, the rest of those digits are meaningless.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 8 de Jul. de 2015
Editada: James Tursa el 8 de Jul. de 2015
Your original number has too many digits to store in a double precision number. E.g.,
num2strexact(73167176531330624919225119674426574742355349194934)
ans =
7.3167176531330625585928818171611107149744948903936e49
So what you are seeing as output is exactly what is being stored (i.e., the exact decimal conversion of the binary bit pattern being stored), and there aren't enough mantissa bits in double precision to store more precision.
In fact, the nearest numbers in the double precision set to your original number are:
>> x = 73167176531330624919225119674426574742355349194934
>> num2strexact(x)
ans =
7.3167176531330625585928818171611107149744948903936e49
>> num2strexact(x+eps(x))
ans =
7.3167176531330635970522535241266364210737607344128e49
>> num2strexact(x-eps(x))
ans =
7.3167176531330615201335101101955850088752290463744e49

5 comentarios

Guillaume
Guillaume el 8 de Jul. de 2015
It has too many digits to store it in any of the core matlab data types! It needs at least 166 bits to represent it as an integer.
You will have to defer to the Symbolic Math Toolbox, or java.BigInteger
James Tursa
James Tursa el 8 de Jul. de 2015
Or vpi in the FEX by John D'Errico.
Kevin
Kevin el 8 de Jul. de 2015
Editada: Kevin el 8 de Jul. de 2015
Thank you for the reply, that does make sense.
But if one is pasting in a large block of numbers, what are the options for defining it as a string besides manually typing in apostrophes.
Perhaps I'll include the problem I am solving to make this clearer: https://projecteuler.net/problem=8
My goal was to create a 1-dimensional array of integers in which each element is a number from that big block of 1000 digits. I figured I could do this simply if the digits were a continuous string and I could then go character by character with str2num (or str2double).
James Tursa
James Tursa el 8 de Jul. de 2015
Well, you can't do a simple copy-paste since MATLAB will assume a double as you found out. So that means a bit more caution on your part. E.g., read as a string directly from the source, or copy-paste with manually adding the apostrophes, or copy-paste into a file and then read the file, etc.
Kevin
Kevin el 8 de Jul. de 2015
Yes, I figured pasting into a file would probably be the proper thing to do but I was hoping there might be some shortcut.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 8 de Jul. de 2015

Comentada:

el 8 de Jul. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by