Use more decimals in Python

前端 未结 2 391
南笙
南笙 2021-01-19 04:17

I already have a code which uses the bisection method to determine the value of something, the problem is that I need a value so precise, more than 15 decimals, and at some

2条回答
  •  不要未来只要你来
    2021-01-19 04:42

    For float instances, Python uses machine-level double precision floating point numbers (double in C terms). What exactly this is depends on the platform and compiler that built Python. These days it is most probably a 64-bit double-precision binary floating-point number. The 53-bit significand gives between 15 to 17 significant decimal digits.

    If you use numpy, you can use the long double floating point number. This has a 64-bit sigificand, yielding approximately 18 decimal digits.

    Decimal values have adjustable precision. By default it is 28 significant digits, but you can change that. So changing to Decimal is probably a good choice if you can get by with the standard mathematical operators and the limited amount of mathematical functions that Decimal supports. But this would indeed mean changing all your code. It is part of the standard library, that might be a plus.

    Alternatively, you could look at the mpmath library. This also allows you to set an arbitrary precision (it defaults to 15 significant digits). It supports much more advanced mathematics than Decimal. It implements all the functions from math and cmath and more. But here you would also have to convert your whole program to change float to the mpf type. By default, mpmath works with integers internally. When available, mpmath uses the Python bindings to the gmp library which makes it much faster, especially at high precision (>100 digits).

    Conclusion

    If you want more precision, you will have to convert your program to use a arbitrary precision math module. Depending on how many math functions you use, mpmath looks like a better fit; it supports all functions from math and cmath and then some.

    How to convert

    A total automatic conversion from float to mpf (or Decimal) types is not possible. But you can get most of the way there.

    Both Decimal and mpf support the standard math operators. So you should not have to change those. You do need to look at places where you create or input floating point numbers and at places where you use functions from math.

    First, change all instances of float( to Decimal( or mpf(. That takes care of explicitly created numbers. Second, search for all floating point literals and replace them the same way. You can use your editor's search-and-replace function for this, or you can write a small Python script for it. So this can largely be automated.

    Then look at all the functions that read data from a source (e.g. a file or database), and update them. This will probably have to be done manually, unless there these explicitly use float(), in which case they are handled by the abovementioned method.

    If you are currently using the math module, you will have to search and replace those functions by their equivalents in the chosen module. In this case the search part can be easily automated. If you have used import math, you can easily search for "math." to find the functions that need replacing. In this case when using mpmath, you could simply replace "math." with "mpmath.", since mpmath implements all functions from math. If you have used from math import x, y, z, you need to search for x, y and z separately. You could even write a script to translate math functions to their equivalents in Decimal or mpf. It depends on the size and complexity of your code base if this is worth doing.

提交回复
热议问题