Consider the below code:
#include <iostream>
using namespace std;
int main ()
{
int a;
double b;
cout << "Enter a number to be divided by three" << endl;
cin >> a;
b = a / 3.0;
cout << "The result of this is:" << b << endl;
return 0;
}
How can I set how many variables I would like to store after the decimal point? This includes above 20 digits.
doubleis a fixed-size type (8 bytes on most systems). So it only stores numbers to a certain precision. There’s no need to fear “infinite division” (in the sense that 1/3.0 has no finite decimal representation).Edit (based on comments below)
If you are actually looking for an arbitrary-precision real number representation, you have to use a library for that, such as Boost.Multiprecision.