Roundoff error - double precision is not enough

  • Thread starter Thread starter coccoinomane
  • Start date Start date
  • Tags Tags
    Error Precision
Click For Summary

Discussion Overview

The discussion revolves around the challenges of computing specific mathematical functions involving sine and cosine, particularly in the context of roundoff errors in double precision arithmetic. Participants explore methods to improve accuracy when both variables, r and x, approach zero, and the implications for numerical integration.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Guido presents functions that exhibit roundoff errors due to the similarity of sine and cosine terms as x approaches zero.
  • Guido mentions a partial solution by relating one function to the first order spherical Bessel function, which can be computed accurately using the GNU Scientific Library.
  • Guido expresses the need for a solution that can handle a large array of r values within a C++ program, indicating that manual computation or pasting results from Mathematica is impractical.
  • One participant suggests using power series expansions for sine and cosine to cancel terms in the numerator for small rx, recommending programming the cubic and possibly fifth-order terms for improved precision.
  • Another participant recommends high precision C++ libraries as a potential solution for the roundoff error issue.
  • Guido acknowledges a previous mistake in coefficient calculations when expanding sine and cosine, which led to improved results upon correction, achieving precision up to 10^-6.
  • Guido expresses skepticism about the effectiveness of high precision libraries unless they have computed the specific integral needed, highlighting the persistent issue of precision loss in differences of small numbers.

Areas of Agreement / Disagreement

Participants generally agree on the existence of roundoff errors in the computations and the potential utility of power series expansions and high precision libraries. However, there is no consensus on the best approach to fully resolve the precision issues, and Guido's concerns about the limitations of available libraries remain unaddressed.

Contextual Notes

Guido's calculations depend on the behavior of sine and cosine functions near zero, and the discussion highlights the unresolved nature of achieving sufficient precision in numerical computations involving small values.

coccoinomane
Messages
19
Reaction score
0
Hi everybody!

I kindly request your help. I have to compute functions like

[tex]\frac{ \sin (r x) - r x \cos (r x)}{r^3}[/tex]
(primitive function of x sin(rx) )

or

[tex]\frac{ -r x (120 - 20 r^2 x^2 + r^4 x^4) cos(r x) + <br /> 5 (24 - 12 r^2 x^2 + r^4 x^4) \sin(r x)}{r^7}[/tex]
(primitive function of x^5 sin(rx) )

when both r and x varies.

The problem with these functions is that the sin and cos factors are very similar to each other when x approaches zero. This is a big issue: double precision is not enough to compute the difference because of roundoff errors or simply because 14-15 digits are not enough to distinguish the two factors.

I kind of solved the problem for the first function. In fact, I could express it up to a factor as the first order spherical bessel function:

[tex]j1(x) = \frac{\sin(x)/x - \cos(x)}{x}[/tex],

which is well computed in the GNU Scientific Library.

I need to calculate those functions to solve the integral I discussed in https://www.physicsforums.com/showthread.php?p=2028408#post2028408 and that uart helped me to solve.

Thank you very much for any suggestion,

Guido
 
Last edited:
Physics news on Phys.org
I forgot to mention that I need to compute the functions for a very large array of "r" values from within a C++ program. Thus, pasting the result from Mathematica is not helpful :)

Cheers,

Guido
 
The problem you have is essentially in the numerators of your expressions when rx is small. I suggest that you (on paper) use the power series for sin and cos, where the terms in rx cancel for your expressions. Programming in the cubic term and possibly the fifth order (depending on how precise you want it) should be sufficient for small rx.
 
or use any of the high precision c++ libraries...
 
Hi mathman, thank you for your answer. You are right indeed.
I tried to expand the sine & cosine as my first approach, but I made a stupid mistake in the calculation of the coefficients and I got wrong results. I re-did everything and now I get results as precise as 10^-6. Thank you!

@NoDoubts
Could you please point me to some of these libraries?
However, I am not sure it would help. Unless they have computed the same function I need, i.e.

[tex] \int_{0}^{k_0} k^{n} \frac{sin{kr}}{kr} dk[/tex]

(and GSL doesn't have), I am afraid the precision problem would pop up again. However precise can the library be, it boils down to a difference between very small numbers --> loss of precision.

Cheers,

Guido
 

Similar threads

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