Python Floating point arithmetic and Fourier collocation

Click For Summary
The discussion highlights issues related to floating-point arithmetic in numerical computations, specifically in the context of Fourier collocation schemes for nonlinear partial differential equations (PDEs) with periodic boundary conditions. A key point raised is the unexpected non-zero imaginary part resulting from the calculation of exp(1j*pi), which should theoretically yield -1. This non-zero component affects the Nyquist frequency coefficient, which ideally should be purely real, leading to significant discrepancies in the spectrum analysis of what is expected to be a uniform steady state. The discussion emphasizes that while libraries like scipy.fft.rfft may overlook these imaginary components, the presence of such roundoff errors can lead to misleading results, underscoring the importance of careful handling of floating-point calculations in scientific computing.
pasmith
Science Advisor
Homework Helper
Messages
3,340
Reaction score
1,883
Python:
>>> from numpy import exp, pi
>>> exp(1j*pi)
(-1+1.2246467991473532e-16j)

The fact that the imaginary part of this is not zero is wrecking a fourier collocation scheme for a nonlinear PDE with periodic boundary conditions: the coefficient corresponding to the Nyquist frequency, which should be purely real, does not remain so. This had no bearing on the physical quantity, because scipy.fft.rfft apparently ignores the imaginary part of this coefficient, but it was disconcerting to look at the spectrum of what was apparently a uniform final steady state and find that the magnitude of this coefficient was on the order of 10^{100}.
 
Technology news on Phys.org
It's that pesky roundoff error again. A simpler illustration is
Python:
>>> from math import sin, pi
>>> sin(pi)
1.2246467991473532e-16
Don't go out into the world of floating point arithmetic without your big boy trousers on.
 
  • Haha
Likes Mark44 and anorlunda

Similar threads

  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K