How can I sum these numbers . and return the result in string datatype without loosing the precision.
'9437256976162618652738646244869425874869', '776357087634731721006'

 채택된 답변

Stephen23
Stephen23 2016년 6월 23일
편집: Stephen23 2016년 6월 23일

1 개 추천

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

Muhammad Usman Saleem
Muhammad Usman Saleem 2016년 6월 23일
what about if we convert them this string to number then add?
I do not know whether author want to find sum of 9437256976162618652738646244869425874869 indiviual no or just these two string?
Guillaume
Guillaume 2016년 6월 23일
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 2016년 6월 23일
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Shameer Parmar
Shameer Parmar 2016년 6월 23일

0 개 추천

a = '9437256976162618652738646244869425874869';
b = '776357087634731721006';
c = num2str(str2num(a) + str2num(b))

댓글 수: 5

Stephen23
Stephen23 2016년 6월 23일
편집: Stephen23 2016년 6월 23일
Note that this answer is incorrect because it does not take into account the limited precision of the IEC 754 floating-point numbers that MATLAB supports. In particular this answer uses the default double class, which only has around 15-17 decimal digits of precision. Converting the string to numeric using str2num gives a double, and a double number simply cannot store all of those digits: it is impossible!
Not only is the precision of double limited, but any arithmetic above the largest consecutive integer will also give rubbish answer:
>> flintmax()
ans =
9.007199254740992e+15
which is waaaaay smaller than the numbers given in the question. Switching to integer classes also does not help, as they are limited to:
>> intmax('uint64')
ans = 18446744073709551615
Would the author like to explain how they managed to convert a string with forty digits into a double, and magically retained all of the digits of precision? (hint: it is impossible, because double only has 15-17 decimal digits of precision).
Guillaume
Guillaume 2016년 6월 23일
It's a shame you can't vote for comment, Stephen's comment gets my +1.
Please test your answer before posting! And please, understand the limits of numeric data types!
Shameer Parmar
Shameer Parmar 2016년 6월 23일
@Guillaume and Stephen:
I know that there is limitation for class double.. I am simply providing the possible answer to the author..and trying to help..
Shame on you.. you people should provide the proper answer instead of posting such negative comments.. Stop posting such negative comment and provide the answer to author..
I know you even dont know the answer... lets see..
Guillaume
Guillaume 2016년 6월 23일
편집: Guillaume 2016년 6월 23일
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 2016년 6월 23일
+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

댓글을 달려면 로그인하십시오.

Aymen Jassam
Aymen Jassam 2016년 6월 23일

0 개 추천

Problem solved convert to double each number individually and sum, with respect the rest (carry).
Thanks a lot to all of you

댓글 수: 1

Stephen23
Stephen23 2016년 6월 24일
편집: Stephen23 2016년 6월 24일
@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.

댓글을 달려면 로그인하십시오.

yousef salah
yousef salah 2019년 3월 12일

0 개 추천

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

카테고리

질문:

2016년 6월 23일

답변:

2019년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by