Roundoff error - double precision is not enough

• coccoinomane
In summary, the problem discussed is how to compute functions involving sin and cos factors that become very similar when x approaches zero. The solution involves using the power series for sin and cos and programming in the cubic and possibly fifth order terms. High precision C++ libraries can also be used, but it may not fully solve the precision issue when dealing with small numbers. The specific function needed is not available in the GNU Scientific Library.
coccoinomane
Hi everybody!

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

$$\frac{ \sin (r x) - r x \cos (r x)}{r^3}$$
(primitive function of x sin(rx) )

or

$$\frac{ -r x (120 - 20 r^2 x^2 + r^4 x^4) cos(r x) + 5 (24 - 12 r^2 x^2 + r^4 x^4) \sin(r x)}{r^7}$$
(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:

$$j1(x) = \frac{\sin(x)/x - \cos(x)}{x}$$,

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:
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...

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.

$$\int_{0}^{k_0} k^{n} \frac{sin{kr}}{kr} dk$$

(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

1. What is roundoff error in double precision?

Roundoff error in double precision refers to the discrepancy between the exact mathematical result of a calculation and the value that is stored in a computer's memory due to the limitations of representing real numbers in binary form. This error can accumulate with multiple calculations, leading to inaccuracies in the final result.

2. Why is double precision not enough to avoid roundoff error?

Double precision is a data type that allows for a higher level of precision compared to single precision, but it still has limitations. This is because real numbers cannot be represented perfectly in binary form, and the more calculations that are performed, the more the roundoff error can accumulate. In complex scientific calculations, a higher precision data type may be necessary to avoid significant errors.

3. How does roundoff error affect scientific calculations?

Roundoff error can lead to inaccuracies in scientific calculations, especially when dealing with large numbers or when performing multiple calculations. In some cases, the errors may be small and may not significantly impact the overall result. However, in other cases, the errors can be significant and may affect the validity of the results.

4. Can roundoff error be completely eliminated?

No, roundoff error cannot be completely eliminated. It is an inherent limitation of representing real numbers in a computer's memory. However, it can be minimized by using higher precision data types or implementing more sophisticated algorithms to reduce the number of calculations and minimize the accumulation of errors.

5. How can scientists mitigate the effects of roundoff error?

To mitigate the effects of roundoff error, scientists can use higher precision data types, such as quadruple precision, whenever possible. They can also implement error analysis techniques to identify and correct any significant errors. Additionally, checking the results with alternative methods or using software packages specifically designed to handle roundoff error can also help mitigate its effects.

• General Math
Replies
2
Views
1K
• General Math
Replies
2
Views
1K
• General Math
Replies
1
Views
7K
• General Math
Replies
1
Views
2K
• General Math
Replies
5
Views
1K
• General Math
Replies
4
Views
810
• Introductory Physics Homework Help
Replies
12
Views
423
• Special and General Relativity
Replies
7
Views
738
• Topology and Analysis
Replies
2
Views
298
• Calculus
Replies
4
Views
843