Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Error due to Floating Point inaccuracies (Documentation) #3

Closed
shivin9 opened this issue May 22, 2017 · 2 comments
Closed

Error due to Floating Point inaccuracies (Documentation) #3

shivin9 opened this issue May 22, 2017 · 2 comments

Comments

@shivin9
Copy link
Contributor

shivin9 commented May 22, 2017

The sum of calculated stencil doesn't come out to be exactly 0 with the error increasing as we increase the order of approximation.
For a stencil for 4th order derivative and 10th order approximation, we have

julia> sum(A.stencil_coefs)
-1.4052778038453617e-14

This is not a problem with Float64 as tested with BigFloat also.
So the solution is to adjust the middle coefficient of the stencil to set the overall sum = 0.
Reference:
SO Post: https://scicomp.stackexchange.com/questions/11249/numerical-derivative-and-finite-difference-coefficients-any-update-of-the-fornb
Improved Foenberg paper: http://epubs.siam.org/doi/pdf/10.1137/S0036144596322507

julia> A.stencil_coefs[7] -= sum(A.stencil_coefs)
1.371740740740740209188154585806529439651058055460453033447265625000000000000000e+01

julia> sum(A.stencil_coefs)
0.000000000000000000000000000000000000000000000000000000000000000000000000000000

julia> res[500]
2.399999481622449663473228831378492031944915652275085449218750000000000000000000e+01

which is what we want.

@shivin9 shivin9 closed this as completed May 22, 2017
@shivin9 shivin9 reopened this May 22, 2017
@shivin9 shivin9 changed the title Error due to Floating Point inaccuracies Error due to Floating Point inaccuracies (Documentation) May 22, 2017
@dextorious
Copy link
Collaborator

Good catch. In general, I've never seen a practical case (like a PDE discretization) where this numerical issue would matter (say, by significantly impacting the numerical stability), but the fix is cheap and there's no reason not to do it.

@shivin9
Copy link
Contributor Author

shivin9 commented May 23, 2017

[Reminder to self]: Use @code_warntype

ChrisRackauckas pushed a commit that referenced this issue Jul 18, 2019
ChrisRackauckas pushed a commit that referenced this issue May 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants