File Exchange

image thumbnail

HPF - a big decimal class

version 1.15 (1.39 MB) by John D'Errico
High precision floating point arithmetic, a new class written in MATLAB


Updated 21 Mar 2018

View License

Very often I see people asking for a tool that offers more than 16 digits or so of accuracy. MATLAB itself only allows you to use doubles or singles in standard arithmetic, so that is the normal limit. The fact is, most of the time, if you can't do it with a double, you are doing something wrong. Good practices of numerical analysis are worth far more than any high precision tool. Even so, there are times when you will have a use for a bit of extra precision. And some of you will just want to play in the huge number sandbox. While some of you may use tools like that written by Ben Barrowes, HPF is written purely in MATLAB, so no compiles are needed. For all of you, whatever your reasons, I offer HPF, a High Precision Floating point tool.
In fact, the reason I wrote HPF was for my own purposes. I wanted to learn to use the classdef tools in MATLAB that were released a few years ago. As well, I wanted to try building such a tool as a natural extension of the VPI tools I wrote some time ago. And I wanted to learn some techniques for working in a high number of digits. The result is HPF.
There are a few ideas I've introduced for how HPF interacts with the user. For example, HPF can work in any number of decimal digits, as chosen by the user. You can set the number of digits as a default. Thus, if you want to always work in 30 decimal digits, with 2 guard digits on all computations, then type this at the command prompt:
DefaultNumberOfDigits 30 2
From now on, for you HPF will always work in a total of 32 decimal digits of precision, and report the top 30 digits, thus two guard digits will be used internally. For example,
pie = hpf('pi')
pie =
exp(pie - 3)
ans =
HPF will recall this state the next time you start MATLAB. You can override the default state by specifying a different number of digits though.
ans =
I've stored values as part of HPF for e and pi that are accurate to 500,000 digits. In fact, those numbers were generated by HPF itself.
Finally, for speed and efficiency, HPF stores all numbers in the form of Migits, which are bundles of decimal digits. This yields a huge bonus in the speed of multiplies, since conv is employed for that purpose. We can see them here:
ans =
[3141 5926 5358 9793 2384 6264 3383 2795]

The nice thing is that the use of Migits will be transparent to most users. But if you want a bit more speed in your multiples, then you can get a boost by typing this:

DefaultDecimalBase 6

From now on, HPF will employ base 1000000 migits internally, what I call 6-migits. The only problem is, you will be restricted from using numbers with more than 36000 decimal digits. Speed has a price.

Another nice use of HPF is to extract the exact decimal form that MATLAB uses to store its own numbers. For example, what number does MATLAB REALLY store internally when you type in something like 1.23?

ans =

Is HPF complete as it stands? Of course not. HPF currently represents nearly 7000 lines of MATLAB code, in the form of dozens of methods available for the class. As it is, you will find many hundreds of hours of work on my part, over the course of several years. But I've not yet written a huge number of things that might be useful to some people. For example: roots, eig, chol, det, rank, backslash, gamma, etc. And HPF offers no support for complex numbers. Even so, I hope that some will find this useful, if only to learn some of the tricks I've employed in the building thereof. Some of those tricks are described in HPF.pdf.

For example, multiplies are best done in MATLAB by conv. But divides take more work, so here I use a Newton scheme that employs only adds and multiplies, and is quadratically convergent. A similar trick is available for square roots.

Or, look into how my exponential function works. Here I've used a few tricks to enhance speed of convergence of the exponential series. Of course, there are obvious range reduction tricks, but I've gone an extra step there. I also employ a different way of summing the series for exponentials (as well as the sine and cosine series) that minimizes divides.

A lot of thought and research has gone into the methods of HPF. Those thoughts are captured in the HPFMod.pdf file, as enhanced by Derek O'Connor. Many thanks there. HPFMod.pdf is sort of a manual too, for those who want to truly understand the tool.

HPF will probably never be in what I consider to be in a final form, as I am sure there are a few bugs still unfound. Even so, the tool is working quite well on the thousands of tests I have performed. For those of you who try HPF out and do find a bug, please send me an e-mail and I will repair it immediately.

Cite As

John D'Errico (2021). HPF - a big decimal class (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012a
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!