NumPy复数精度疑惑及运算稳定性相关技术问询
Let's tackle your questions about complex number precision in Python (specifically with NumPy 1.14.3 and Python 2.7.14) one by one.
First, here's your code formatted properly for reference:
import numpy as np s = 2 l = 5 v = np.array([np.exp(1j*2*np.pi/l)]) A = pow(s*v, l) + s*v # Print the precision of np.complex128 print np.finfo(np.complex128).precision # Export using 20 decimal places for real and imaginary parts np.savetxt('A.csv', A, fmt=['%.20e%+.20ej'], delimiter=',')
Why do extra decimal digits appear when the reported precision is 15?
The key misunderstanding here is what np.finfo(np.complex128).precision represents: it's the number of significant digits (not decimal places) that can be stored accurately.
np.complex128 consists of two float64 values (one for the real component, one for the imaginary). A float64 can reliably represent ~15-17 significant digits. When you print or export 20 decimal places, the digits beyond those 15-17 are just meaningless noise—they're either artifacts from converting the binary floating-point value to a decimal string, or leftover rounding errors from your calculations. These extra digits don't add any actual precision; they're not trustworthy.
For example, if your imaginary part shows as 0.12345678901234567890, only the first 15-17 digits are accurate. The rest are just padding from the conversion process.
How to quantify complex number precision in Python, and what's the maximum possible?
Let's break this down by the tools at your disposal:
Standard precision options
- Python's native
complextype &np.complex128: Both usefloat64for their real and imaginary parts, so they offer ~15-17 significant digits of precision. This is the default workhorse for most numerical tasks. np.complex64: Usesfloat32components, providing ~6-9 significant digits. Only useful when memory is tight and precision isn't critical.np.complex256(platform-dependent): Usesfloat128components, delivering ~33-36 significant digits. Note that this isn't available on all platforms (Windows systems often lack support forfloat128).
Why your results vary across runs
Floating-point math inherently has small rounding errors. When you chain many operations (like matrix inversions, especially on ill-conditioned matrices), these errors can accumulate. Minor differences in operation order or low-level NumPy implementation details can lead to noticeable variations in results for precision-sensitive tasks.
Getting higher precision
If you need more precision than complex128 can offer, consider these options:
- Use
np.complex256: If your platform supports it, this is the simplest way to get higher precision while keeping NumPy's familiar syntax and performance. - Python's
decimalmodule: This standard library module lets you define arbitrary precision for decimal arithmetic. It's slower than NumPy and doesn't natively support matrix operations, but it's great for high-precision scalar calculations. mpmathlibrary: A dedicated high-precision numerical library that supports arbitrary-precision complex numbers, along with advanced operations like matrix inversions. It's ideal for tasks where precision is the top priority, though it's slower than NumPy.
内容的提问来源于stack exchange,提问作者mgus

