#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

int main () {
    cout << "\nAccumulation of many roundoff errors"
         << "\n------------------------------------"
         << "\nAdd x to itself N times, and"
         << "\nthen divide the result by N"
         << "\n\nEnter value of x: " << flush;
    double x;
    cin >> x;
    cout << "Enter number of times N: " << flush;
    int N;
    cin >> N;
    double ans = x;
    float y = x, fans = x;
    for (int i = 1; i < N; i++) {
        ans += x;
        fans += y;
    }
    ans /= N;
    fans /= N;
    cout.precision(16);
    cout.setf(ios::left);
    cout << "\n         Double Precision        Single Precision   "
         << "\n----------------------------------------------------"
         << "\nAnswer   " << setw(22) << ans
         << "  " << setw(22) << fans
         << "\nError    " << setw(22) << (ans - x)
         << "  " << setw(22) << (fans - y)
         << "\nEpsilon  " << setw(22) << numeric_limits<double>::epsilon()
         << setw(22) << numeric_limits<float>::epsilon() << endl;
}

