How would you make sure the Square root of a variable is without decimals? I want to find the square root of something, but I want the sqrt to be even. The number which will have the operation done on it will be just about any number.
So: root:36 = 6, I want to make sure the sqrt isn't an irrational or rational number. In other words, I don't want a number like 3.445697... or 3.5555 . Also, please explain the code to me, so I know what I'm doing. Please and thank you! ^_^
There are two ways I am interpreting your request, but am assuming you would like the square root of any given number to be a whole number. Is this what you wanted?
If so, quick and dirty: (int)sqrt((double)37);
The above involves casting which may or may not be the best answer for you as some information could be lost and the rounding of numbers may also not be what you desire.
If it is of any concern, keep in mind casting as I did would also output sqrt(45) as 6.
If you want to find only perfect squares, you could compare the results of the function verses the same results casted to an int to check for whole numbers, like so:
1 2 3 4 5 6
double var = sqrt(someNumber);
if (var==int(var))
//is a perfect sqaure
else
//isn't
This technique is naive; it may or may not work. (Depends on range of someNumber and the value representation of double):
1 2 3 4 5 6
double var = sqrt(someNumber);
if (var==int(var))
//is a perfect sqaure
else
//isn't
Try:
1 2 3 4 5 6 7 8 9
int main()
{
for( longlong number = 4611686018427387900LL ; number < 4611686018427387908LL ; ++number )
{
double root = std::sqrt( double(number) ) ;
if( root == (longlong) (root) )
std::cout << number << " is a perfect square\n" ;
}
}
Output on my implementation:
1 2 3 4 5 6 7 8
4611686018427387900 is a perfect square
4611686018427387901 is a perfect square
4611686018427387902 is a perfect square
4611686018427387903 is a perfect square
4611686018427387904 is a perfect square
4611686018427387905 is a perfect square
4611686018427387906 is a perfect square
4611686018427387907 is a perfect square
Portability would require something like this:
1 2 3 4 5 6 7 8 9
bool is_perfect_asquare( longlong number )
{
if( number < 0 ) returnfalse ;
double root = std::sqrt( double(number) ) ;
longlong a = (longlong)(root) ;
longlong b = (longlong)(root+1) ;
return ( number == a*a ) || ( number == b*b ) ;
}
root = sqrt(number); //assuming number and root are doubles
perfect = root * root;
if(perfect == number)
//perfect root
else
The square root of 5 is an irrational number, so it doesn't really exist as square root, but it gets close. If you ever multiply the square root of five times itself as you see it on the calculator, you won't ever get five. Most scientific calculators will probably return five because they store it as the square root of five (when you use them to find the square root of five), but your computer won't work the same way.
In case anyone was wondering, I found a way to verify there are no decimals in the square root. Round them off using a cast to int, and square them. If the result is the same as the original, then you know no decimals exist in the number. This is because if you divide a number by an integer, and it results in a decimal, when you round and square it, you won't get the original number. You will get somewhere above or below it.