0

I'm dealing with arbitrarily large numbers in MATLAB, I've been futzing with a script for a while and finally found out that my problem wasn't a logic error.

It was this:

>> 2^63 - 1 == 2^63

ans = 

     1

In MATLAB 2012a, without additional libraries, is there a way to handle large numbers of up to 2^100?

Edit:
Apparently, the precision error happens as soon as 2^54.

Second Edit:
According to Peter, I can use vpa. This actually solves the problem quite nicely:

>> vpa(sym(2^63)) - vpa(sym(2^63) - 1)

ans =

1.0
Community
  • 1
  • 1
Axoren
  • 623
  • 1
  • 8
  • 22
  • Possible duplicate: http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Peter Jan 14 '15 at 16:36
  • 1
    You're near. If you're representing an integer number with a floating point variable then maximum integer number you can represent without errors is 2^53. See [flintmax](http://it.mathworks.com/help/matlab/ref/flintmax.html). – Adriano Repetti Jan 14 '15 at 16:37
  • 2
    Depends on what you mean by "additional libraries". The Symbolic Math Toolbox provides `vpa` (variable precision arithmetic) for this – Peter Jan 14 '15 at 16:38
  • @AdrianoRepetti I used the following line of code to see when the problem starts: `[(1:63)', [2.^(1:63)'] == [2.^(1:63)' - 1]]`. – Axoren Jan 14 '15 at 16:41
  • @Peter I don't have the luxury of including additional Toolboxes. VPA is installed and it works great. If you post that as an answer, I'll accept it. – Axoren Jan 14 '15 at 16:43

2 Answers2

3

The command vpa is provided as part of the symbolic math toolbox. It performs "infinite precision" math using a number representation other than CPU-native floating point values. This does mean, however, that arithmetic will be MUCH slower, and that numbers will take up much more space.

Peter
  • 14,559
  • 35
  • 55
  • 1
    `vpa` has no infinite precision, but you can specify the precision you need. Check `digits`. If `vpa` is available this is definitely the better solution. – Daniel Jan 14 '15 at 17:14
1

Matlab comes with an JRE. Use the java class java.math.BigDecimal to work around.

Daniel
  • 36,610
  • 3
  • 36
  • 69