File Exchange

image thumbnail

Multiple Precision Toolbox for MATLAB

version (110 KB) by Ben Barrowes
This toolbox defines a new mp class allowing multiple precision objects in MATLAB.


Updated 13 Dec 2018

View Version History

View License

The mp toolbox defines a new class, the mp class, which holds arbitrary precision quantities. Many common numerical functions are overloaded for this class and therefore work without modification to source code. Look at the @mp directory under the MATLAB directory for a list of mp supported functions. If the function is not specifically written for mp objects, it still may work if the function in question relies only on functions in the @mp directory. Precompiled and builtin function from TMW like eig, etc. will not work with mp objects unless specifically written using overloaded functions from the @mp directory.A simple script, mp_makeall.m looks through the current variables,and converts all doubles to mp objects.The mp toolbox is only implemented for base 10 quantities, and the rounding mode is fixed to be GMP_RNDN (unless you change it in all the /private/*.c files and recompile).The overloaded functions sum, min, and max only work for 1 or 2 dimensional mp objects right random numbers can be generated using rand() if rand at least 1 of the input arguments is an mp object. However, the seed given to gmp_randseed_ui is only in the range 0<seed<1000000, but this can be adjusted in @mp/rand.mA zeta function using m arithmetic is provided, whereas native MATLAB has no such function except for sym objects.

Cite As

Ben Barrowes (2021). Multiple Precision Toolbox for MATLAB (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (52)


Carl Witthoft

Dunno what typo that was, but is not what you meant. how about

Neal Morgan

Please update the instructions to install this in windows 10 and Matlab R2019b. I got MinGW and it's library download options included gmp and mpfr. I tried using those dll's in place of ones from the old website links in the readme (one of the readme links is clearly to something completely unrelated).

Umut Sayin

I would really like to see a roots and poly function in this set since this commands have serious issues with low precision while calculating roots or polynomials

Sergio Sanz

I've downloaded the last version but I can't find any updated instructions to install it on Windows 10.
Could anybody tell me something about this?
Thanx in advance. :-)

Ben Barrowes

WRT Pavel's comment. I zeroed out digits past 15 or 16 so that the mp number would "look" like the double. But perhaps he is right that I should not do this. In mp.m, mpfr_construct_dd calls mpfr_init_set_d which eventually sets the multiple precision number to the number nearest the double rounded in the direction GMP_RNDN. As he says, mp_TESTING seems to run fine with those lines commented.

I have commented out the lines he suggested and uploaded a new version.

yu xiao

@tao length @Theo How to set Xcode?thank you
my platform is:
OS X 10.10.5
Matlab R2016b
Xcode 7.0.1
And typing: “clang -v”, I see
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
typing: “mex -setup”,it's ok.
but when typing" mp_compile_all", i see
compiling: mex gmp_prototype.c -lmpfr -lgmp -I/usr/local/include/ -L/usr/local/lib/
Compile with 'Xcode with Clang'.
Misuse of mex
/Users/MATLAB/waveform/mptoolbox_1.2/@mp/private/gmp_prototype.c:2:10: error: 'mpfr.h' file not
found with <angled> include; use "quotes" instead
#include <mpfr.h>
In file included from /Users/MATLAB/waveform/mptoolbox_1.2/@mp/private/gmp_prototype.c:2:
/Users/MATLAB/waveform/mptoolbox_1.2/@mp/private/mpfr.h:52:10: error: 'gmp.h' file not found with
<angled> include; use "quotes" instead
#include <gmp.h>
2 errors generated.

How to set Xcode?

Pavel Kyshtymov

Nice tool, but there is a ridiculous bug that leads to imprecise conversion of double precision numbers to mp.
Here is a simple demo:
>> x = -1e-1-9.9e-16;
>> abs(x-double(mp(x,1000)))/eps(x)
ans =
A deliberately selected double precision number is converted forth to mp with 1000 bits of precision and back to double precision. Resulting value differs from the original one by 71 ULP!
It is likely that this issue is resolved by removing lines 39-41 and 46-48 of mp..m. The author zeroes 17:end digits in a string representation of the input number, assuming that the double precision provides no more than ~16 accurate digits. Due to inherent misalignment of base-10 and base-2 representations, such a voluntaristic truncation may result in an error of up to 1e-16/eps(1e-1) ~= 7.2 ULP, Moreover, the code does not account for the minus char that is present for a negative value, in which case the mp number is left with only 15 digits of precision and tenfold error bound of ~72 ULP.
I don't understand the motivation behind this truncation. Simple removal of this step does not change results of mp_TESTING and mp_TESTING2 scripts. Whether of not it influences the memory/efficiency footprint - I haven't conducted any experiments on this..


Hi, with Matlab 2016b under Linux I get the following error:

Assignment between unlike types is not allowed.
Error in mp/subsasgn (line 6)
Error in mp_myqr (line 78)

Error in mp/qr (line 7)
Error in mp_mysvd (line 265)

Error in mp/svd (line 6)
Error in mp/norm (line 61)
Error in mp_TESTING2 (line 8)
c_mp=norm(asin(sin(a42))-a42);disp(['2.1 sin/asin' setstr(9) num2str(c_mp)])


I want to thank Ben Barrowes for creating this toolbox and everyone contributing for more tips for installation.

Is there a mp version for Eigenvalues and eigenvectors eig?

tao length

I successfully installed the package on
OS X 10.11.4
Matlab R2015b
Xcode 7.3.1

The install steps:
1. Install Xcode and command-line-tools
2. Install gmp and fpmr lib.
e.g. brew install xxx
3. modify mexopt so that matlab could find compiler

4. Download mptoolbox and unpack.
5. !!! NOTE !!!
The compilation is done NOT in /Applications/MATLABxxx as the install instruction suggested, but in the unpacked folder.
Go into private dir, modify the mp_complie_all.m as mac install instruction said,
and change the mex command as @Theo suggested
mex([dd(ii).name], '-I/opt/local/include/', '-L/opt/local/lib/', '-lmpfr', '-lgmp')
and then in matlab, run it under private dir.

6. Go back to unpacked dir,
now we can use mp_TESTING.m and mp_TESTING2.m

7. If you want to use mptoolbox somewhere else, make sure the unpacked dir is in your load path, or the dir where @mp is in your load path. Otherwise, matlab can't find the mp class.

Lourenço Peixoto

I have followed the "Step-by-step instructions" to installation on a Mac (@mp/install_mac).

My platform is:
OS X El Capitan 10.11.3

I have installed XCode 7.2.1

When I type "sudo /usr/sbin/gcc_select 3.3" on Terminal Window I see:

sudo: /usr/sbin/gcc_select: command not found

And typing: “gcc -v”, I see

Configured with: --prefix=/Applications/ --with-gxx-include-dir=/Applications/
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix


I want to thank Ben Barrowes for creating this toolbox. It seems to work very well for my purposes. I want to make a few notes:

1. I managed to successfully get this working on OSX 10.10 using Matlab 2015a. There are a few changes I note in the install_mac instructions.

a) It's easy to use Macports to install gmp and mpfr. You will need to change the linking directories to /opt/local instead of /usr/local (run "port contents mpfr" to check where it has installed)

b) The syntax adjustment calling the mex command did not work for me. I had to use instead (notice arguments are passed one by one in strings)

mex([dd(ii).name], '-I/opt/local/include/', '-L/opt/local/lib/', '-lmpfr', '-lgmp')

2. I also forgot to mention: the install_mac instructions suggest that you need to downgrade the version of gcc. I did not have any issues using the stock compiler through XCode, and it is likely that the instructions are outdated.

To be more precise, when I type 'clang -v' I get

Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)

After installing gmp and mpfr, I did not have to make any selection via gcc_select. The mex command seems to work fine on my version of clang.

3. Finally, I want to just comment that I think it is in poor taste that the developer for a paid toolbox is commenting in this page to people who need help, in order to advertise his product. For my purposes, this free product (mptoolbox) worked just fine. Give it a shot.


I also forgot to mention: the install_mac instructions suggest that you need to downgrade the version of gcc. I did not have any issues using the stock compiler through XCode, and it is likely that the instructions are outdated.

To be more precise, when I type 'clang -v' I get

Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)

After installing gmp and mpfr, I did not have to make any selection via gcc_select. The mex command seems to work fine on my version of clang.

Benjamin K.

Unfortunately, this does not compile on my Mac. When running install_mac, I get (the [...] indicate the path of the mp toolbox):

[...]/mptoolbox_1.2/@mp/install_mac ; exit;
[...]/mptoolbox_1.2/@mp/install_mac: line 1: No such file or directory
[...]/mptoolbox_1.2/@mp/install_mac: line 7: Installation: command not found
[...]/mptoolbox_1.2/@mp/install_mac: line 9: syntax error near unexpected token `('
[...]/mptoolbox_1.2/@mp/install_mac: line 9: `PowerBook G4 (12" PowerBook6,1 867 MHz PowerPC G4 3.3)'


Sascha D. Krauß


When I run mp_compile_all.m I encounter with following message:


C:\MinGW\bin\dlltool: Can't open def file: ../../extern/include/libmex.def

ans =


C:\MinGW\bin\dlltool: Can't open def file: ../../extern/include/libmat.def

ans =


C:\MinGW\bin\dlltool: Can't open def file: ../../extern/include/libmx.def

ans =


The syntax of the command is incorrect.

ans =


The syntax of the command is incorrect.

ans =


The syntax of the command is incorrect.

ans =


compiling: gmp_prototype.c

txt =

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ogmp_prototype.dll -ID:\MATLAB\R2012a\extern\include -I. -L. gmp_prototype.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ogmp_prototype.dll -ID:\MATLAB\R2012a\extern\include -I. -L. gmp_prototype.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm
compiling: mpfr_absc.c

txt =

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_absc.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_absc.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_absc.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_absc.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm
compiling: mpfr_add.c

txt =

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_add.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_add.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_add.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_add.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm
compiling: mpfr_atan2.c

txt =

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_atan2.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_atan2.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_atan2.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_atan2.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm
compiling: mpfr_ceil.c

txt =

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_ceil.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_ceil.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_ceil.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_ceil.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm
compiling: mpfr_const_log2.c

txt =

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_const_log2.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_const_log2.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm

!C:\MinGW\bin\gcc -g -O2 -shared -fexceptions -ompfr_const_log2.dll -ID:\MATLAB\R2012a\extern\include -I. -L. mpfr_const_log2.c gmp.dll mpfr.dll -lmat -lmex -lmx -lm
Operation terminated by user during mp_compile_all (line 54)
Then when I run the mp_TESTING.m I encounter eith the same problem of Sascha Krauß . Can anyone help me?

Sascha D. Krauß

I'm sorry, I haven't used it for a year and can't remember the solution. (Or even the problem.) Maybe I can have another look, when I'm back in office on Monday, but I don't think, that it will help much...


Hi, I have downloaded this toolbox but I dont know how it works. I have set the path of extracted toolbox in my Matlab's search path but it doesnt work!
Actually I have the same problem of Haula Dias.

Hasula Dias

Hi Sascha, can you please explain how you fixed that problem ? I'm getting the same error when I try to run the test program,i.e,

Undefined function or method 'mpfr_construct_dd' for input
arguments of type 'double'

I don't see a mfile with the same name ?!..

Sascha D. Krauß

Problem solved.

(Why is there no edit or delete function?)

Sascha D. Krauß

When I try the examples from the readme file, I get the following error. Why? (GMP and MPFR are installed. I use Ubuntu 11.10 and Matlab R2011b 64-bit.)

>> ymp=mp(2,1000)
Undefined function 'mpfr_construct_dd' for input arguments of type 'double'.

Error in mp (line 27)

Pavel Holoborodko

@Ahmed Fasih
Try Multiprecision Computing Toolbox from here:

It supports 32 and 64-bit versions of Windows and the latest Matlabs. Easily installed in a few seconds, no compilation required - everything are installed and tuned automatically for the best performance on your CPU.

Mas OS X (64-bit) and Linux (32 & 64-bit) are also supported. No compilation is required.

Multiprecision Computing Toolbox from Advanpix supports much wider set of MATLAB functions missing in the submission, including eig, det, cond, fft/ifft, chol, fzero, null to name a few. Many advanced numerical methods are supported too - matrix functions, optimization, ODE, root finding, special functions, numerical integration, etc....

Ahmed Fasih

Any updates on getting this easily going on Win64 with R2011 or R2012? The pre-compiled binaries are DLLs, I can't seem to load them with newer Matlabs, while the compilation seems to need .def files (libmex.def, libmat.def, etc.) which are unavailable in newer Matlabs.

David Goldsmith

Also, is the cast of a struct into a class the only sense in which this thing is a class? That's kind of weak.

David Goldsmith

"The mp toolbox is only implemented for base 10 quantities"

Does this mean that the IEEE 754-2008 32-bit base 2 format, which results in a variable number of ~7.225 digits precision in decimal, can't be represented precisely?

Christo Fogelberg


Anyone have this working on the latest mac os x (10.6.4) and matlab r2010a? or have compiled win64 libraries?

Paul Hur

This is awesome. It will greatly help me with calculation of my equations. However, I cannot find a function for gammainc. Is there anybody who has this function of window version and helps me?

Dmitry Savransky

Absolutely fantastic. Exactly what I needed - many thanks. Quick note for anyone installing on RHEL5. The default rpms that may be pre-installed for GMP don't seem to work when it comes time to compile the c files. However, if you get rid of these and compile GMP and MPFR from source, everything works great.

Nigel Delaney

I just reinstalled this package for a windows 7 64 bit computer (running 64 bit matlab). It was a bit of work so I thought I would share the process, or offer to email anyone the binaries/toolbox as I have it.
The first issue is that building mpfr/gmp for x64 is quite hard. Apparently, there is a new ming compiler out for 64 bit, but no one has had it work so far (and I didn't try it). The second option, SUA is also useless as there is no gcc compiler for x64 with it, and you would be forced to make a 32 bit version. So, in order to get the libraries I had to use MPIR (which can substitute for GMP) and then compile mpfr with this instead of gmp. To do this, you need to do the following:
Download MPFR ad MPIR :
Now, both of these are hard to build, but someone has setup a way to build them with either visual studio or visual studio express that is reasonably painless. Go here to get the build instructions.
Note that in order to get the visual studio build to work, you have to install a yasm assembler program and integerate it into visual studio (follow the 3 step instructions on their website).
One you are setup to go, first build mpir using visual studio. Note that there is a vcbuild directory included with the mpir program that you can use directly to make the files. This program can substitute for gmp in the next linking step, so use the gmp.h header file that comes with it next. With that done, you can now open the VS project from the gladman site and get ready to compile mpfr. Note that the project files he hands out do not include the actual code, so you will have to move the mpfr source files and reference mpir library files around to make sure they appear in their correct locations relative to the what is in the .sln file gladwell gives out. Once this is done, you are all set to build and things should compile just fine.
Next, compile the .mex files (which should be pretty straightforward now that the libraries are built). Now the package is up and running, however it did not work right away for me. It appears that for whatever reason if matlab saw this type of code:
in the toolbox .m files, it would call the eq.m method of this toolbox instead of the builtin method, and this broke the dang thing as it would not know how to work with two double types. The same was true for <, >, etc.. This seemed very strange to me and I am not sure if it is due to matlab upgrades or to some odd system setting on my machine. In any event, changing the relevant files to check for a double and go to the builtin method seemed to work around the problem okay, and so far the code is working fine.

Great package overall!!! Certainly a lot of work must have gone into it, only giving it 4 out of 5 because I just spent 6 hours getting it to work, but I am sure if I had gone the 32 bit route it would have been much easier.

Brent Ware

Anyone had any luck getting this to work with recent versions of OS X? The old instructions don't seem to work.

Kevin Mitchell

Beautiful! This rivals laprint.m ( for packages that really should be included with Matlab by default.

Kevin Mitchell


grat c

can anyone tell me how exactly to install the binaries in windows? thanks

Zolotykh Nikolai

Nikolai Zolotykh

It's really useful and greate soft

Ronan Fleming

When installing in suse 10.1, I found it necessary to explicitly provide the path '/usr/local/lib' in the mp_compile_all.m file as that is where my lmpfr libraries were

e.g. If you get the error
mex: link of 'mpfr_something.mexglx' failed.

Then change lines 12 & 13 to

disp(['compiling: mex ',dd(ii).name,' -L/usr/local/lib -lmpfr -lgmp'])
mex([dd(ii).name],'-L/usr/local/lib -lmpfr -lgmp');

Andrew Wildes

Installed on windows, and works well. It's slow, so it would be difficult to use with any fitting program. We found a couple of bugs and contacted Mr. Barrowes, who was very receptive and replied quickly and enthusiastically. Recommended!

Laszlo Sragner

Some small precisions (1-2) can cause Matlab to break. Some operations cause memory dump errors , maybe this is due to passing illegal arguments somewhere to mex functions.
The GMP mex wrapper functions work well with the supported dlls on a windows XP for these it is worth using it and writing your own m-class around them.

Martin Andersson

I got this system working well on xp-based Matlab, R13 but I never got the compilation to work, an error about missing h-files came up. Anyway, there is no need to compile it as it worked fine using the dll files from Lopes. Before I could use it, I needed to put some work on it a bit and rewrite a few of the m-files, but now it seems OK.

Yes, like other pointed out already, the calculations do become very, very slow, but at least I became able to see what happens when using more digits.

Slower as expected, but still an Excellent tool. (But the Windows installation guide is difficult to understand).


Christoph Krautz

Installation fails on Windows! Documentation is more than bad!

Umberto .

Really excellent. However it can be improved, e.g.: (i) some functions are really slow when applied to large matrices (e.g. the overloaded function "prod") and (ii) it worked for me under Matlab 7.3 (2006b) but not under Matlab 6.5. I give a five star rating since this toolbox fills a serious gap into the Matlab numeric representation capabilities.

Carlos López

I were able to compile it for R13 under Windows, but failed miserably to do so for 7.0 and 7.2.
If anyone needs such version contact me to describe what I did.
I added some other test to the toolbox, and overall it appears to run fine.

Egemen Kolemen

For those who are interested in using gcc of cgywin please see:

Jarek Grzegogrze³kowski

Leo da Li

Well, in Linux (Fedora Code 4 and Ubuntu) the installation was successful. Actually, on the line 7 of the file mp_compile_all.m:

mex([dd(ii).name,' mpfr_atan2.c'],'-lmpfr -lgmp');

i had to delete ",' mpfr_atan2.c'", otherwise an error is produced. But well, it works pretty great ;) But it would be nice if some functins were included or completed. For exemple, the mrdivide.m function is not fully functional, so we can't accomplish operations where inv of a matrix is required. Usually, mrdivide(A,B) does the same as A*inv(B), but hugely faster and more accurate (inv is neither included).

But, well, this toolbox works really fine. I rate 4 just because it is not fully functional (yet).

Leo da Li

It really seams that this toolbox, as it is, was made to work uniquely under matlab for linux. I tried many diferent things to compile it in matlab for windows (xp) and i couldn't! It could be realy helpfull if instructions/modifications for matlab in windows were provided! I used gcc of cygwin to compile gmp and mpfr, but i can't make a mexopts.bat to gcc on matlab for windows!

Tim Eakin

I have the same problems encountered by Zhiyi when attempting installation on a Windows XP platform. It looks like there may need to be some sort of -I or -L mex switch in a mexopts.bat file. I use Lcc for the C compiler.

Zhiyi Li

I can not compile the C files in it using mex. The compiler can't find mpfr.h

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!