Calculating a Sum in C: Finding the Error in Floating Point Precision?

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Sum
Click For Summary
SUMMARY

The forum discussion focuses on calculating the sum of the series [1 + sum{1/(i(i+1)) from 1 to n}] in C, highlighting issues with floating-point precision. Users discovered that using "float" data types resulted in significant rounding errors, particularly when n is large, while "double" provided more accurate results. The discussion emphasizes the importance of using appropriate data types to avoid overflow and precision loss in calculations, especially when dealing with large integers in floating-point arithmetic.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of floating-point arithmetic
  • Knowledge of data types: float vs. double
  • Basic mathematical series and summation techniques
NEXT STEPS
  • Explore C data types and their precision limits, focusing on "float" and "double".
  • Learn about floating-point overflow and underflow in C programming.
  • Investigate numerical methods for improving precision in calculations.
  • Study the implications of rounding errors in iterative calculations.
USEFUL FOR

Programmers, especially those working with numerical computations in C, mathematicians interested in numerical analysis, and anyone seeking to understand floating-point precision issues in programming.

  • #31
I found the abolute value of (pi^2)/6-the result(for forward method:1.6439348459243774,for backward method:1.6439344882965088).
 
Physics news on Phys.org
  • #32
mathmari said:
I found the abolute value of (pi^2)/6-the result(for forward method:1.6439348459243774,for backward method:1.6439344882965088).

Right.
So apparently the forward method gives a result that is higher in this case than the backward result.
However, the true result that we should have for n=1000 is best approximated by the double backward algorithm, which you already gave.

Code:
float forward   = 1.6439348459243774
float backward  = 1.6439344882965088
double backward = 1.6439345666815597

As you can see the "float backward" result is closer to the double result than the "float forward" result.
However, we do see that the "float forward" result is higher than the "float backward" result.
This would happen due to rounding errors that work out differently.
 
  • #33
So,isn't the error of the backward method bigger for n<=1000??
 
  • #34
mathmari said:
So,isn't the error of the backward method bigger for n<=1000??

The difference with $\frac{\pi^2}6$ of the backward method is bigger in this case (presumably due to "bad" rounding in the forward method that turns out to be "lucky").

However, the error in the backward result is smaller, since the result is not supposed to be $\frac{\pi^2}6$. It needs many more iterations before it gets there.
 
  • #35
If you multiply your result with 6 and find the square root and then the error,how could we explain why the forward method gives better results for smaller n?? :o:confused:
 
  • #36
mathmari said:
If you multiply your result with 6 and find the square root and then the error,how could we explain why the forward method gives better results for smaller n??

Lucky bad rounding.

Suppose we do the calculation with 2 significant decimal digits for n=4.
Then we get:
forward: ((1+0.25)+0.11)+0.063 = 1.5
backward: 1+(0.25+(0.11+0.063)) = 1.4

As you can see, the forward result is closer to $\pi^2/6 \approx 1.64$.
That is because of the lucky bad rounding of 0.25 to 0.3 and of 0.063 to 0.1.

Note that after iteration n=4, the forward result does not increase anymore (the next term is 0.04). But the backward result keeps getting closer to the desired end result.
 
  • #37
Thank you very very much! ;) :D
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
7
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 31 ·
2
Replies
31
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
4
Views
2K