-1

I've been trying to find a way to store the result of this arithmetic for hours now.

I understand that the data is too large to be stored. But the largest data type my compiler seems to support is unsigned long long.

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

int main() {
   int num1 = 100000;
   int num2 = 200000;
   int num3 = 300000;
   double num4 = 500000.00;
   unsigned long long product = (num1 * num2 * num3 * num4);

   cout << fixed << setprecision(3) << product << endl;

   // Answer should be 3000000000000000000000.000
   // Result is 18446447556077551616
   return 0;
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
Tomarik
  • 75
  • 1
  • 7
  • What you probably need is a library that supports arbitrarily large integers and rational numbers thereof. (Or implement your own) – ph3rin Jan 13 '20 at 06:37
  • Does this answer your question? [How to store extremely large numbers?](https://stackoverflow.com/questions/18060242/how-to-store-extremely-large-numbers) – NaShBe Jan 13 '20 at 06:38
  • https://stackoverflow.com/questions/3340511/what-is-the-simplest-way-of-implementing-bigint-in-c – Abhineet Jan 13 '20 at 06:38
  • Well, my main constraint is that I have to use an online compiler because this is a class. – Tomarik Jan 13 '20 at 06:38
  • Maybe use BigIntegers? I remember using them in Java to solve a Project Euler problem. (for numbers excessively large in size) –  Jan 13 '20 at 06:43
  • Was that actually the result? It didn't end in `5`? – M.M Jan 13 '20 at 07:06

2 Answers2

6

Assumption: ints are 32-bit. long longs are 64-bit.

The largest value a 64-bit unsigned can be is:

  18446744073709551615

You are trying to compute:

3000000000000000000000

Which won't fit into any 64-bit integer (signed or unsigned)

Let's also break down the problem:

num1, num2, num3 are ints, and the expression of multiplying all three ints together

   num1 *   num2 *   num3
 100000 * 200000 * 300000

As an integer expression, num1*num2*num3 is already overflowing with the values you are multiplying with. And that's before we've multiplied by num4.

Better solution would be to declare num1, num2, and num3 as long long so that the above expression will be evaluated as the same type.

But even with this fix, the multiplication of 6 quadrillion times 500000.0 gets evaluated as a double. That still overflows both double and the 64-bit signed range.

Short answer: You're going to need a big number math library to evaluate numbers in this range.

selbie
  • 100,020
  • 15
  • 103
  • 173
1

here is how you really do it convert high numbers with notation or add variable counters.

#include <iostream>
#include <iomanip>
using namespace std;
#include <stdio.h>
#include <math.h>

int main() {
     int num1;
     int num2;
     int num3;
     int num4;
     int check1;
     double check2;

     cin >> num1 >> num2 >> num3 >> num4;
   
     cout << (num1 * num2 * num3 * num4) << " " << (num1 + num2 + num3 + num4)/4 << endl;
    
     check2 = static_cast<double>((num1 + num2 + num3 + num4)/4.000);
   
if (num1 > (pow(10, 4))){
      num1 = num1 / (pow(10, 5));
      num2 = num2 / (pow(10, 5));
      num3 = num3 / (pow(10, 5));
      num4 = num4 / (pow(10, 5));//note math to add 0 is (5+5+5+5) 20 (0)s
      check1 = (num1 * num2 * num3 * num4);
      cout << check1 << "00000000000000000000.000";
      cout<< setprecision(3) << fixed;
      cout << " " << static_cast<double>(check2) << endl;
   }
   else{
      check1 = (num1 * num2 * num3 * num4)+ .00001; 
      cout<< setprecision(3) << fixed;  
      cout << static_cast<double>(check1) << " " << static_cast<double>(check2) << endl;
}
   return 0;
}
joe
  • 11
  • 1