Sum of two large Floating-point numbers

• Difficulty Level : Medium
• Last Updated : 01 Jul, 2020

Given two very large floating-point numbers in form of large strings str1 and str2, the task is to add the given two numbers.

Example:

Input: str1 = “584506134.87368350839565308”, str2 = “30598657.0330473560587475634983”
Output: 615104791.9067308644544006434983

Input: str1 = “38.30”, str2 = “37.0983”
Output: 75.3983

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To find the addition of two large integers that can’t be stored in the inbuilt data type we will use an array to store the digits of the numbers and then perform the addition digit by digit starting from the LSB.

Using this concept, we can also find the summation of large floating-point numbers.

Steps to add the two given floating-point numbers:

1. Split both the given floating-point number in form of a string with respect to the decimal point to separate the fractional and integer part of the numbers.
2. Add the fractional and integer part of the two numbers separately and forward the final carry part of fractional addition to integers part.
For Example:
str1 = "23.94" and str2 = "34.23"

For fractional part:
f1[] = {4, 9}
f2[] = {3, 2}
--------------
Sum  = {7, 1, 1}
Therefore, Carry = 1

For Integer part:
Carry = 1
I1[] = {3, 2}
I2[] = {4, 3}
--------------
Sum  = {8, 5}

3. Concatenate the digits stored for integer and fractional part with a decimal ‘.’ to get the required sum two large floating point numbers.
From Integer part = 58
From fractional part = 17

Sum = 58.17

Below is the implementation of the above approach:

 // C++ program to find Sum of two// large Floating-point numbers  #include using namespace std;  // Function to make fractional part// with equal digitsvoid makeEqualAtFront(vector& A,                      vector& B){    int n = A.size();    int m = B.size();    int diff = abs(n - m);      if (n < m) {        for (int i = 0; i < diff; i++) {            A.insert(A.begin(), 0);        }    }    else {        for (int i = 0; i < diff; i++) {            B.insert(B.begin(), 0);        }    }}  // Function to make Integral part// with equal digitsvoid makeEqualAtback(vector& A,                     vector& B){    int n = A.size();    int m = B.size();    int diff = abs(n - m);      if (n < m) {        for (int i = 0; i < diff; i++) {            A.push_back(0);        }    }    else {        for (int i = 0; i < diff; i++) {            B.push_back(0);        }    }}  // Function to add the given large// floating point number stringvoid findSum(string s1, string s2){      int i;      // To store the integer and    // fractional part of numbers    vector Ints1, Ints2;    vector Fracs1, Fracs2;      // Separating integer and    // fractional part of s1    for (i = s1.length() - 1; i > -1; i--) {          // If decimal occurs break        if (s1[i] == '.') {            break;        }        Fracs1.push_back(s1[i] - '0');    }      i--;    for (; i > -1; i--) {        Ints1.push_back(s1[i] - '0');    }      // Separating integer and    // fractional part of s2    for (i = s2.length() - 1; i > -1; i--) {          // If decimal occurs break        if (s2[i] == '.') {            break;        }        Fracs2.push_back(s2[i] - '0');    }      i--;    for (; i > -1; i--) {        Ints2.push_back(s2[i] - '0');    }      // Making number of digits in    // fractional and Integer    // part equal    makeEqualAtFront(Fracs1, Fracs2);    makeEqualAtback(Ints1, Ints2);      // Adding fractional parts of    // s1 and s2    int n = Fracs1.size();    int m = Fracs2.size();    i = 0;    int carry = 0;      while (i < n && i < m) {          // Traverse the Fracs1[] and        // Fracs2[] and add the digit        // and store the carry        int sum = Fracs1[i]                  + Fracs2[i]                  + carry;          Fracs1[i] = sum % 10;        carry = sum / 10;        i++;    }      int N = Ints1.size();    int M = Ints2.size();    i = 0;      // Adding integer part of    // s1 and s2    while (i < N && i < M) {        int sum = Ints1[i]                  + Ints2[i]                  + carry;        Ints1[i] = sum % 10;        carry = sum / 10;        i++;    }    if (carry != 0)        Ints1.push_back(carry);      // Print the result by appending    // Integer and decimal part stored    // in Ints1[] and Fracs1[]    for (int i = Ints1.size() - 1; i > -1; i--) {        cout << Ints1[i];    }    cout << '.';    for (int i = Fracs1.size() - 1; i > -1; i--) {        cout << Fracs1[i];    }}  // Driver Codeint main(){    string str1        = "584506134.87368350839565308";    string str2        = "30598657.0330473560587475634983";      findSum(str1, str2);      return 0;}
Output:
615104791.9067308644544006434983

My Personal Notes arrow_drop_up