2892. Sum of values

 

Find the sum of values for the function

in some integers..

 

Input. First line contains the number of points n (1 ≤ n ≤ 50). Next line contains n integers x1, x2, ..., xn – the points where the function must be calculated and summed up (0 ≤ |xi| ≤ 109).

 

Output. Print one number – the sum of function values f(x) at given points. The answer is correct if its accuracy is no less than 10-9.

 

Sample Input

3

1 2 3

 

Sample Output

7.83333333333333

 

 

SOLUTION

mathematics

 

Algorithm analysis

If you compute the specified sum directly into a variable, you can get a rounding error. For example value

double y = 1000000000 + 1.0 / 1000000000;

equals to 1000000000 (fractional part does not fit in double). At the same time the value

double y = 1.0 / 1000000000;

equals to 1e-9.

To minimize the rounding errors in calculations, we calculate the integer and fractional part of the sum separately in different variables.

 

Algorithm realization

Calculate the integer part of the sum in the variable sum1, the fractional part in the variable sum2.

 

scanf ("%d", &n);

sum1 = sum2 = 0;

for (i = 0; i < n; i++)

{

  scanf ("%lf", &x);

  sum1 += x;

  sum2 += 1.0 / x;

}

 

Print the desired result as the sum of two variables.

 

printf ("%.12lf\n", sum1 + sum2);