10

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 ?

slezadav
  • 6,104
  • 7
  • 40
  • 61
  • 1
    There'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 Answers4

5

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 to 2^53-1 (check documentation of bitmax function) (~10^15, while your numbers are on the order of 10^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.

angainor
  • 11,760
  • 2
  • 36
  • 56
3

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.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
3

Adding to the answer, if you need more digits of accuracy, you can try to use this fex file

bla
  • 25,846
  • 10
  • 70
  • 101
0

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.

Argyll
  • 8,591
  • 4
  • 25
  • 46