Improve the precision when dealing with integers of very high order
Show older comments
I am dealing with numbers which are of the order of 50th power of 10. After solving the numbers with such high magnitude, I am looking for an answer of the order of 100 or less. The default precision in Matlab is not enough so that I do not get the correct answers. Can anyone suggest a best method to improve the precision up to the level I need?
For example, the answer for 123456789012345678 - 123456789012345677 should be one, but Matlab does not give that answer.
Accepted Answer
More Answers (2)
KSSV
on 20 Dec 2016
a = int64(123456789012345678) ;
b = int64(123456789012345677) ;
c = a-b
8 Comments
Lakshitha Kumarapperuma
on 20 Dec 2016
KSSV
on 20 Dec 2016
I am surprised about your problem application. Where and why you are dealing with such huge numbers. As suggested by Walter you ha e to go for symbolic calculations.
Lakshitha Kumarapperuma
on 20 Dec 2016
John BG
on 20 Dec 2016
Lakshitha
Giving a pointer or link to a text that may (or may not) contain the answer to a question is not really an answer.
Some people try to hit as many questions as possible by not really providing answers but only pointers to where they think the answer lays.
That is not a good practice because it's like, upon being asked
Q: how to change a Ford Fiesta damaged catalytic converter?
A: go and have a look into the Ford Fiesta service manual
1.- One didn't really have to post an answer in a forum, one could go straight to the service manual, but most likely, the asking entity has already checked the service manual, didn't understand it, or even couldn't find the right service manual, and for any of such reasons the asking entity resorts to an online forum.
2.- using pointers to point at answers instead of providing answers increases the risk of ending up running in circles.
So, Lakshitha,
for these reasons I kindly ask you to check my answer to your question uploaded just a few minutes ago.
If you find I am helping solve the question please mark my answer as accepted, thanks in advance
John BG
Guillaume
on 20 Dec 2016
"Giving a pointer or link to a text that may (or may not) contain the answer to a question is not really an answer."
To each their opinion. It is assumed Lakshitha can read. Pointers to thoroughly documented and fully tested implementations that can perform a lot more than addition and subtraction are a lot better than half baked, slow, homemade code.
John BG
on 20 Dec 2016
Oh yeah :) people can read the whole Encyclopedia Britannica yet they may wonder if you put them on the right direction
Walter Roberson
on 20 Dec 2016
John D'Errico is the author of the package I posted the link to. I have no doubt that it is a comprehensive, efficient, and very well tested package.
Lakshitha
you are asking a very interesting question.
PC machines come with standard narrow and fixed number formats.
With these formats one cannot implement ciphering and authentication operations used on very long numbers.
To free a machine from the basic narrow number types, one way is to put the numbers in files.
The following code does what you want, just for rest operation, but first you have to put the 2 numbers into 2 separate files, as strings: n1.txt contains '12345678912345678' and n2.txt contains '12345678912345677'
no headers no spaces, just the strings containing the arbitrary long positive integers.
% arbitrary legnth basic arithmetics: + - * /
% -
fid_n1=fopen('n1.txt');fid_n2=fopen('n2.txt');
cs_n1=textscan(fid_n1,'%s'); % files to cells
cs_n2=textscan(fid_n2,'%s');
s_n1=cell2mat(cs_n1{1,1}); % cells to strings
s_n2=cell2mat(cs_n2{1,1});
L_n1=numel(s_n1);L_n2=numel(s_n2);dL=L_n1-L_n2
w=0; % carry
s_n3=' ';
if L_n1>=L_n2 L=L_n1
for k=L:-1:dL+1
v=str2num(s_n1(k))-str2num(s_n2(k-dL))+w;
s_n3=[s_n3 num2str(v)]; % building n3 backwards
if v>=0 w=0; end % modify carry for next loop
if v<0 v=10-abs(v);w=-1; end
end
else L=L_n2
for k=L:-1:dL+1
v=str2num(s_n2(k))-str2num(s_n1(k-dL))+w;
s_n3=[s_n3 num2str(v)]; % building n3 backwards
if v>=0 w=0; end % modify carry for next loop
if v<0 v=10-abs(v);w=-1; end
end
end
s_n3(1)=[]; % remove the initial void space
s_n3=s_n3([end:-1:1]);
k=1;while s_n3(k)=='0' s_n3(k)=[]; end; % remove void nulls left of most significant digit
% write 3rd resulting number into file
fid_n3=fopen('n3.txt','w')
fprintf(fid_n3,'%s',s_n3)
fclose(fid_n3);
fclose(fid_n1);fclose(fid_n2);
the other 3 basic operations: sum multiplication and division can be implemented in a similar way.
And one can also refine for sign integers, or any other format of interest like complex, and floats.
Other ways to free machines from narrow fixed number types involves using dedicated hardware.
There are also C libraries that implement arbitrary length arithmetic, but to use them in MATLAB one should either translate them or call such functions from MATLAB.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help, click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Things I've tried that don't work
1.- vpa(n1)-vpa(n2)
2.- str2num('123445678912345678')-str2num('123445678912345677') ans = 0
3.- vpa(str2num('123445678912345678'),20)-vpa(str2num('123445678912345677'),20) ans = 0.0
4 Comments
Guillaume
on 20 Dec 2016
If you're going to claim that your solution is better than existing solutions (vpi, java, .net), you should at the very least provide the implementation of of multiplication and division (particularly, the latter one)
John BG
on 20 Dec 2016
last time I checked vpi toolbox, it crashed, reported to D'Ericco, he fixed it. Java? are we talking MATLAB or Java? .NET again, why always off the tangent? focus on the question
Guillaume
on 20 Dec 2016
The following is pure matlab code:
n1 = java.math.BigInteger('12345678912345678');
n2 = java.math.BigInteger('12345678912345677');
n1.subtract(n2)
NET.addAssembly('System.Numerics');
n1 = System.Numerics.BigInteger('12345678912345678');
n2 = System.Numerics.BigInteger('12345678912345677');
ToString(n1 - n2)
Either implementation (and vpi) offer a lot more than basic operations, with log, modulo, power, gcd, etc.
John BG
on 21 Dec 2016
So why didn't you explain at once all this knowledge you seem to have to the person asking the question?
Categories
Find more on Time Series Events in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!