Advertisement
kot_mapku3

Sum two double numbers with increased accuracy

Oct 9th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     ios::sync_with_stdio(false);
  10.     cin.tie(nullptr);
  11.     cout.tie(nullptr);
  12.  
  13.     string str;
  14.     long long f = 0, q = 0;
  15.     int fl = 0, ql = 0;
  16.  
  17.     while (getline(cin, str)) {
  18.         int i = 0;
  19.         string tmp;
  20.         for (; str[i] != '.'; ++i) {
  21.             tmp = tmp + str[i];
  22.         }
  23.         i++;
  24.         f += stoll(tmp);
  25.         tmp = "";
  26.         for (; i < str.length(); ++i) {
  27.             tmp = tmp + str[i];
  28.         }
  29.         q += stoll(tmp);
  30.     }
  31.     fl = to_string(f).length();
  32.     f += q / pow(10, 15);
  33.     q = q % (long long)pow(10, 15);
  34.     ql = to_string(q).length();
  35.  
  36.     cout << f << ".";
  37.     for (int j = 0; j < 15 - ql; ++j) {
  38.         cout << 0;
  39.     }
  40.     cout << q;
  41.     //cout << setprecision(15);
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement