Improve the precision when dealing with integers of very high order

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

3 Comments

Thanks Walter. I think this should give me the answer. I will update the status once I test this solution in my equations.
Another option (particularly if you don't have the symbolic toolbox) is to go through Java BigDecimal (for real numbers) or Java BigInteger (for integers) both of which you can instantiate and manipulate directly in matlab.
On Windows, you also have .Net BigInteger.
Personally, I don't like the java.BigDecimal implementation. I originally considered using it as the engine under the hood for my HPF toolbox. Actually, I wrote that entire toolbox three times, with three different computational engines. I found what I considered some serious flaws in java.BigDecimal, so I chose the current HPF implementation.
However, the java.BigInteger implementation is viable, but it too works far better with a better interface. So then I rebuilt VPI, creating VPIJ, which does work, for those who need a speed bump over VPI. I'll post it one day.

Sign in to comment.

More Answers (2)

a = int64(123456789012345678) ;
b = int64(123456789012345677) ;
c = a-b

8 Comments

Thanks for the answer. Yes it works for the example given. But, in fact, my example was not enough to display the complexity of the problem. What I need is some thing like the following.
a1 = int64(12345678901234567890123456789012345678901234567890123456789); a2 = int64(12345678901234567890123456789012345678901234567890123456788); a1-a2 should be one, but int64 also gives a zero. Any idea about a better method than int64?
Thanks.
Looks like you need to learn about data types. You can look at the maximum values that can be stored as integers in Matlab here.
Otherwise, you should go with what Walter suggests.
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.
What Walter has suggested should give me the answers I guess. I will test it with my equations and update it here. I am doing my PhD in Quantum Nano-Optics and we usually deal with such numbers and require very high precision in numerical calculations. Thanks for your answers too.
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
"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.
Oh yeah :) people can read the whole Encyclopedia Britannica yet they may wonder if you put them on the right direction
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.

Sign in to comment.

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

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)
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
Huh? Maybe ... learn matlab integration with java
The following is pure matlab code:
n1 = java.math.BigInteger('12345678912345678');
n2 = java.math.BigInteger('12345678912345677');
n1.subtract(n2)
or .Net, again pure matlab code:
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.
So why didn't you explain at once all this knowledge you seem to have to the person asking the question?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!