# str2double with long string seems to give wrong answer

5 vues (au cours des 30 derniers jours)
Philip Masding le 29 Fév 2024
Commenté : VBBV le 1 Mar 2024
Run in MATLAB on line
str='6879331413876961408';
num=str2double(str);
fprintf('%i',num)
6879331413876961280
6879331413876961280
##### 2 commentairesAfficher AucuneMasquer Aucune
Stephen23 le 29 Fév 2024
"str2double with long string seems to give wrong answer"
No, it gives the correct answer: it gives you the DOUBLE value that is closest to that specified in the string.
Lets phrase it a different way: can you show us a DOUBLE value that is closer that specified in the string? (hint: no)
Lets try it right now:
str = '6879331413876961408';
num = str2double(str);
hex = num2hex(num)
hex = '43d7de12de40149c'
next_higher = hex2num('43d7de12de40149d');
next_lower = hex2num('43d7de12de40149b');
fprintf('%.0f\n',next_lower,num,next_higher)
6879331413876960256 6879331413876961280 6879331413876962304
We can see that 1408 is closer to 1280 than either 0256 or 2304.
So your hypothesis that "str2double with long string seems to give wrong answer" is easily demonstrated to be incorrect. What is much much more likely is that you have not considered the implications of using finite precision binary floating point numbers.
Dyuman Joshi le 29 Fév 2024
Description from str2double documentation - "X = str2double(str) converts the text in str to double precision values."
Also, note the data type of the output obtained in Walter's answer.

Connectez-vous pour commenter.

### Réponse acceptée

Walter Roberson le 29 Fév 2024
format long g
str = '6879331413876961408';
num = sscanf(str, '%ld')
num = int64
6879331413876961408
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Plus de réponses (2)

John D'Errico le 29 Fév 2024
Modifié(e) : John D'Errico le 29 Fév 2024
Do you appreciate that str2double will convert the number to a DOUBLE PRECISION number? Of course it must, as why would it convert to something other than a double? MATLAB does not by default work in arbitrarily high precision. If it did, it would be relatively as slow as molasses on too many computations. And then people (maybe even you) would be complaining at how slow it was. So MATLAB uses doubles for almost all computations, unless you specifically use some other class. (syms, for example could store that number, or my own VPI or HPF classes.)
What is the maximum integer a double can store? (Exactly)
flintmax
ans = 9.0072e+15
which is 2^53-1, in case you care. Numbers large than flintmax, but less than realmax will see only the top (approximately) 16 digits retained. Anything below that becomes computational garbage. Is your number larger than flintmax?
6879331413876961408/flintmax
ans = 763.7592
So it is too large, by a factor of almost 1000. Note that the 3 least significant digits were wrong. Should that be a surprise here if the number if too large by a factor of 1000 to fit into a double exactly?
If you truly needed to store that number in some other class and you don't want to use the alternatives I mentioned, it turns out tht int64 or uint64 could do it exactly. For example:
6879331413876961408 < intmax('int64')
ans = logical
1
uint64(6879331413876961408)
ans = uint64 6879331413876961408
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

VBBV le 29 Fév 2024
```fprintf('%d',num)
```
##### 6 commentairesAfficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
Dyuman Joshi le 29 Fév 2024
@VBBV, you can not use a format specifier for str2double. The input (if convertible to a double precision value) will always be converted to a double precision value (otherwise NaN will be the output).
VBBV le 1 Mar 2024
str='6879331413876961408';
% num=str2double(str)
fprintf('%s',str)
6879331413876961408
As @Dyuman Joshi mentioned that using str2double and/or str2num functions will result in double precision values which are limited by significant 16 digits. @Philip Masding you can get same value if you use format specfifier for string %s

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by