3

I am trying to convert the numeric value as string or character using num2str or sprintf. I don't understand why the output is different than my input? I expect the output to be the same as the input.

s=num2str(180814132242864695,'%18d')

s = 180814132242864704  % this is the output

s = sprintf('%18d',180814132242864695)
s = 180814132242864704
Paolo
  • 21,270
  • 6
  • 38
  • 69
Alemex
  • 145
  • 1
  • 1
  • 11
  • I think [this post on MATLAB answers](https://uk.mathworks.com/matlabcentral/answers/127210-why-does-mat2str-0-005-90-return-0-0050000000000000001-in-matlab#answer_134616) will explain this – Paolo Aug 14 '18 at 21:59
  • 1
    See also https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab – Luis Mendo Aug 14 '18 at 22:00
  • 1
    Just for grins, try `a = uint64(18081413)*1e10 + 2242864695` :-) – beaker Aug 14 '18 at 22:06
  • 1
    IMO it was a poor design choice of MATLAB to not include notation for integer literals. – jodag Aug 14 '18 at 22:21
  • 1
    ^this. you'd think `uint64(180814132242864695)` would be sufficient. but noooo. :/ – beaker Aug 14 '18 at 22:22
  • @jodag Your edit is incorrect, `char` is correct, `string` is not. – Paolo Aug 14 '18 at 22:38
  • I edited the title to use the term "string" in the generic programming sense [to mean a sequence of characters](https://en.wikipedia.org/wiki/String_(computer_science)). The purpose of the edit was to improve the likelihood of this question showing up in a search engine where *most* programmers are likely to use the term "string" instead of "character vector". As an aside, "string" is intermittently used in the documentation for both [`sprintf`](https://www.mathworks.com/help/matlab/ref/sprintf.html) and [`num2str`](https://www.mathworks.com/help/matlab/ref/num2str.html). – jodag Aug 14 '18 at 22:50
  • Yes I agree with you, but this question should be more visible to MATLAB users, who should distinguish the difference between character vector and string. Yeah, the documentation for those functions should be improved. – Paolo Aug 14 '18 at 22:52

1 Answers1

3

MATLAB interprets all numeric literals as double precision floating point. The problem isn't with the conversion to a string but rather with the fact that when converted into memory the value 180814132242864695 becomes the nearest value which can be represented with 64-bit floating point precision which is consequently 180814132242864704.

Generally speaking, if you're working with numbers which require more than 52 bits of precision to represent (for example integers larger than 2^53 - 1 = 9007199254740991 ~ 9e15) you're going to start running into precision issues.

Example:

>> 9007199254740992 == 9007199254740993
ans =
    logical
       1

The answer linked by Luis Mendo Why is 24.0000 not equal to 24.0000 in MATLAB? gives a more detailed description of the floating point representation used by MATLAB (and most languages).

jodag
  • 19,885
  • 5
  • 47
  • 66
  • 1
    I would include two examples: `s=num2str(1808141322428646,'%18d') s=num2str(18081413224286469,'%18d')` to show OP that the maximum precision is determined by the number of digits. – Paolo Aug 14 '18 at 22:05