In C++, I want to get the square root of a big int. The number is less than the maximum integer represented as a double, so this approach should work. The ttmath library doesn't have a square root function.
I'm using the ttmath library for big ints.
I wrote the following program:
#include <ttmath/ttmath.h>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
ttmath::UInt<4> a = "234567890123456789012345";
// convert BigInt to double
double d=(double)a;
// then take the square root of the double
double b = sqrt(d);
cout << b << endl;
}
Compiling it results in the following error the following error:
cannot convert 'ttmath::UInt<3>' to 'double' without a conversion operator
double d=(double)a;
So it doesn't like using a cast for converting to a double.
What is the required conversion operator?
The ttmath library doesn't directly allow converting
IntorUInttodouble. You need to go throughttmath::BigThat being said, you don't need to convert to
doubleto compute the square root or print.You can also compute the integer square root with
a.Sqrt(), but this wouldn't give you any fractional digits.