Open Metric
Science, Fiction and Me

Today I Learned

C++ int Multiplication

#include <iostream>

using namespace std;

int main(int argc, char *argv[]){

   // Check the number of parameters
   if (argc < 1) {
      // Tell the user how to run the program
      std::cerr << "Usage: " << argv[0] << " takes 1 parameter:  the value of int to be multiplied " << std::endl;
      return 1;
   }

   int a = stoi(argv[1]);
   long int b = stol(argv[1]);

   cout.precision(10);
   cout << "Multiple itself as int: " << a * a << endl;
   cout << "Multiple itself as long int: " << b * b << endl;

}

Compile and run the program using -std=c++11.

cpp-int-multiply ‹master*›  ./int-multiplication.out 1000
Multiple itself as int: 1000000
Multiple itself as long int: 1000000

cpp-int-multiply ‹master*›  ./int-multiplication.out 10000
Multiple itself as int: 100000000
Multiple itself as long int: 100000000

cpp-int-multiply ‹master*›  ./int-multiplication.out 100000
Multiple itself as int: 1410065408
Multiple itself as long int: 10000000000

cpp-int-multiply ‹master*›  ./int-multiplication.out 1000000
Multiple itself as int: -727379968
Multiple itself as long int: 1000000000000

The results seems to be weird because the multiplicated number exceeds the max of int.

Within for loops, the int i = 0 should be used carefully. The calculation i*i is problematic even i is not exceeding the max. There are solutions to this. We could convert i to double (double)i in situ. Or we could simple use long int.

Source code at repl.it.

Results from asciinema.

A Random #TIL# for You


Most technical TILs will be updated at datumorphism.leima.is/til

By OctoMiao

Last updated