Python Floating point arithmetic and Fourier collocation

AI Thread 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,336
Reaction score
1,877
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
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
7
Views
4K
Replies
4
Views
5K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
13
Views
3K
Replies
1
Views
5K
Back
Top