As far as I know, the accuracy of the type of float
in python
limited to 16 decimal places. I need about 40 decimal places. And the question is how to increase the number of decimal places in the type of float
, or what data type to use instead of float, for which operations with complex numbers are defined?
4 answers
GMPY project is not satisfied? Binding to gmp and mpfr.
- An interesting thing, where you can read more and how I understand the documentation in Russian is not? - eiyawii
- Well, the docks themselves are in English and very short, the gmp library and others (mpfr, mpri) are also in Aglitsky, but in short there is something in Russian somewhere. There are tests in the packages. They are not so much in English as in python. - alexlz
- How to connect the library to the python? it is written in C, simple import does not work. And in general the project is too old - the latest changes to 2006 are. - eiyawii
- Frankly, where the figure for 2006 is is not clear. gmpy2-2.0.0a2 - June 8, gmpy-1.14 - November 2010. The Windows binary should connect themselves (hopefully), and
sudo python setup.py install
under the linux. Only the main libraries (gmplib or others) should not be forgotten. - alexlz - @eiyawii: gmpy2 supports the MPFR (float) and MPC (complex) libraries. Most recent change of March 8, 2015 (less than a month ago) - jfs
I can also advise the package mpmath ; Numpy supports float128, but that's about 32 characters. In general, the issue with accuracy is very relevant, there is a very simple example of floating-point calculations (in Python) , which makes you think that you need to be very careful about floating-point calculations ...
Generally speaking, the Decimal class is used for more accurate calculations in Python. You can read more about it here. In this class, you can control the accuracy of calculations, there is no loss of values ​​in it, unlike double, but, of course, you should not expect high speed work.
- The fact is that Decimal is not friendly with either the float or the complex. and of course it's perfect, I'll write a test problem and post it to show where the error occurs. also considered the Fraction type, and I must say it came up, all operations with this type go through, except for calculating the logarithm - if the argument of the function log10 is a Fraction type number, then an exception is raised - eiyawii
- Losses in Decimal also occur - the difference is that the base is decimal (rather than binary) and you can set your own accuracy (rather than fixed (for the given platform, compiler). And so close standards related to floating-point arithmetic are used. - jfs
I'm afraid, but I'll have to emulate them myself. The fact is that the default type in Python is Double, which is 8 bytes in size, 52 bits of which are used for mantissa, which give 15-16 characters in decimal form. To provide 40 decimal places, you need somewhere 132 bits only for the mantissa. And another 12-15 bit for the exhibitor. Total 144-147 bits (18-19 bytes). I do not know libraries that operate with such numbers.
If you clarify the task, why do you need such accuracy, maybe I will give a nice solution to the problem. But in ordinary tasks it is very rarely needed. In my life I met a task only once, in which I needed an accuracy of 12 decimal places, and then, as it turned out, it was a synthetic task that would cut off those who use a calculator and cannot think.
- And if the calculator gives an accuracy of more than 12 characters? I apologize for the offtopic. And as a matter of fact - read about the long arithmetic. - Stas Litvinenko
- Most calculators just have an accuracy of 10-12 characters. (I'm talking about physical calculators, not those of operating systems). - KoVadim 4:08
- And what tell about Decimal, Fraction? Both types can represent a number fairly accurately. But having tried to use these types, I ran into problems - Decimal is not friendly with float and complex, and Fraction cannot be an argument of such functions as log10. I would like to say that the task is also synthetic, but necessary for clarity. Later I will write a test problem in which errors occur when applying these types and lay out here. - eiyawii
- Well, if the snag is only in the logarithm, then it can be counted with the necessary accuracy. Formulas can be peeped in the textbook on algebra or here . - KoVadim Nov.