3

I need high precision for a project I'm working on. The problem I have is illustrated here when substracting:

>> 1-0.9999999999999999

ans = 1.1102e-16

>> 1-0.99999999999999999

ans = 0

I know it's related to the double precision. Is there anyway I could get a higher precision? I checked the "vpa" function but I can't manage to get higher precision. Could someone help me with this?

Thank you in advance!

user53534
  • 31
  • 3
  • Do you have an installation of Matlab's Symbolic Math Toolbox ? – High Performance Mark Oct 20 '14 at 16:03
  • 5
    Try `vpa('1-0.9999999999999999')` (note quotation marks). `vpa(1-0.9999999999999999)` won't work because `0.9999999999999999` is first converted to `double` – Luis Mendo Oct 20 '14 at 16:06
  • Minor note: `vpa` requires the Symbolic Math Toolbox, which High Performance Mark is inevitably referring to in the end. Also good job Luis! – rayryeng Oct 20 '14 at 16:18
  • There are other languages that provide arbitrary precision accuracy, like python. – serigado Oct 20 '14 at 19:02
  • there's also HPF by *John D'Errico*: http://www.mathworks.com/matlabcentral/fileexchange/36534-hpf-a-big-decimal-class, or the MEX-wrappers around GMP/MPFR by *Ben Barrowes*: http://www.mathworks.com/matlabcentral/fileexchange/6446-multiple-precision-toolbox-for-matlab – Amro Oct 20 '14 at 19:18
  • You could use `long double`'s in mex functions. – AnonSubmitter85 Oct 21 '14 at 04:58
  • 2
    @AnonSubmitter85: unfortunately, [`long double`](https://en.wikipedia.org/wiki/Long_double)'s are not necessarily portable, VC++ maps `long double` to simply `double`: http://msdn.microsoft.com/en-us/library/9cx8xs15.aspx. Although in reality `double` computations are performed in 80-bit registers if done on the FPU (the native format of x87), but they are stored in 64-bit when transferred to memory. There is an instruction that affects whether intermediate results are reduced to 64-bit after each computation. – Amro Oct 21 '14 at 08:58

1 Answers1

0

Matlab internally uses the 64-bit system double for all its numbers, which explains the precision issues. There do exist extensions for matlab that allow you to use arbitrary precision arithmetic. See for example this question.

Community
  • 1
  • 1
aphid
  • 1,135
  • 7
  • 20