How to sum strings digits?

14 vues (au cours des 30 derniers jours)
Aymen Jassam
Aymen Jassam le 23 Juin 2016
How can I sum these numbers . and return the result in string datatype without loosing the precision.
'9437256976162618652738646244869425874869', '776357087634731721006'

Réponse acceptée

Stephen23
Stephen23 le 23 Juin 2016
Modifié(e) : Stephen23 le 23 Juin 2016
Use John D'Errico's excellent FEX submission, Variable Precision Integer Arithmetic:
>> A = vpi('9437256976162618652738646244869425874869')
A =
9437256976162618652738646244869425874869
>> B = vpi('776357087634731721006')
B =
776357087634731721006
>> A + B
ans =
9437256976162618653515003332504157595875
  3 commentaires
Guillaume
Guillaume le 23 Juin 2016
As per Stephen's comment to Shameer answer, you cannot convert the string to number (double or uint64) simply because these numbers are much much too big to be represented in numeric formats without loss of precision.
Muhammad Usman Saleem
Muhammad Usman Saleem le 23 Juin 2016
Thanks

Connectez-vous pour commenter.

Plus de réponses (3)

Shameer Parmar
Shameer Parmar le 23 Juin 2016
a = '9437256976162618652738646244869425874869';
b = '776357087634731721006';
c = num2str(str2num(a) + str2num(b))
  5 commentaires
Guillaume
Guillaume le 23 Juin 2016
Modifié(e) : Guillaume le 23 Juin 2016
Stephen has provided the proper answer. If you're going to provide an answer that is clearly wrong, we'll point it out. Please note, that we do not make any personal attacks, so refrain from doing so.
You clearly do not understand the limitations of double numbers. Because of the difference of magnitude between a and b, your addition does not do anything at all
a = '9437256976162618652738646244869425874869';
b = '776357087634731721006';
aa = str2num(a);
bb = str2num(b);
cc = aa + bb;
See that cc is equal to aa:
isequal(cc, aa)
returns true. Adding bb made no difference!
That is because the next double number greater than aa is
>>eps(aa)
ans =
1.2089e+24
about 1e24 more than aa whereas bb is four orders of magnitude (only 7e20) smaller. bb is so tiny compared to aa that it makes no difference when it's added.
Muhammad Usman Saleem
Muhammad Usman Saleem le 23 Juin 2016
+1 for Stephon... Who work freely to point out our mistakes... To me he is one of the great source of learning for all researchers...
Pay respect for your seniors

Connectez-vous pour commenter.


Aymen Jassam
Aymen Jassam le 23 Juin 2016
Problem solved convert to double each number individually and sum, with respect the rest (carry).
Thanks a lot to all of you
  1 commentaire
Stephen23
Stephen23 le 24 Juin 2016
Modifié(e) : Stephen23 le 24 Juin 2016
@Aymen Jassam: converting to double most definitely does not "solve" your original question: while converting to double will give an answer, it does not calculate "without loosing the precision" as your question required. This was demonstrated in Guillaume's comment, and explained in my comment.

Connectez-vous pour commenter.


yousef salah
yousef salah le 12 Mar 2019
hello everyone
How can I sum these numbers . and return the result in string datatype without loosing the precision.
'21fade58b','1'
result:
'21fade58c'

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by