I have a question out of sheer curiosity. How is multiplication of large numbers implemented in MATLAB ? Is it Karatsuba,Toom-3, Fürer or something entirely different ?
-
1There's an [interesting thread](http://www.mathworks.com/matlabcentral/newsreader/view_thread/164051) that I've found in MATLAB Central that may be related to your problem... – Eitan T Oct 26 '12 at 18:41
-
Thanks ! that certainly is interesing. Now I know I can use symbolic toolbox to make this kind of operation. Unfortunately it doesn't say anything about how exactly it is implemented or what algorithm is used. Anyway, thanks for comment ! – slezadav Oct 27 '12 at 10:18
-
If you do not *aim for accuracy, but for speed*, then what are your large numbers? Can they be 'approximately' represented by doubles? like 1e254? What is big? Do you need arbitrary precision arithmetic? You say you want large number multiplication, but then you say you do not care about accuracy. What does that mean? – angainor Oct 27 '12 at 22:07
-
[There was a thread about large numbers in MATLAB](http://stackoverflow.com/questions/9337342/matlab-computations-involving-large-numbers). How is that different from what you need? – angainor Oct 27 '12 at 22:09
-
Let's say I am only interested in algorithm, which is used when I try to compute something like this: 139676498390139139676498390676498390*8745566554641239676498390676498390 – slezadav Oct 28 '12 at 06:31
4 Answers
If you are interested in the algorithm for computing for example 139676498390139139676498390676498390*8745566554641239676498390676498390
, then this is what happens:
- both numbers are converted to
double
in IEEE® Standard 754 - as such, the representation is not accurate, because
doubles
can only accurately represent integers of up to2^53-1
(check documentation ofbitmax
function) (~10^15
, while your numbers are on the order of10^35
). - multiplication is performed using double precision floating point numbers, and is thus also approximate
Have a look at this example code:
>> a=139676498390139139676498390676498390;
>> num2str(a)
ans =
139676498390139141600015724509659136
which is clearly not exactly the value you assigned to a
- only the first 16 digits match.

- 11,760
- 2
- 36
- 56
There is no built-in BigInteger
class, if that is what you mean. You can either use the fixed point toolbox, or import relevant java/.NET classes.
By default, numbers are represented in IEEE double precision floating point format.

- 20,795
- 11
- 69
- 104
If your numbers are only big, but you don't require a large number of significant digits. You can multiple a small number with each multiplication, such as $10^-1$. Keep track of the number of multiplications $N$ and obtain a value in terms of $10^{-N}$. This can be a temporary work around.

- 8,591
- 4
- 25
- 46