473,542 Members | 16,813 Online

# Checking if the square root result is integer

2 New Member
Hi, i am trying to create the function, that will check if the sqrt is integer.
My code is below but it doesnt work.
Thanks

int square_check (int x, int y )
{
float z= sqrt(x+y);

if (z ==(int)z)
return 1;
else
return 0;

}// end function
Oct 4 '08 #1
7 14470
boxfish
469 Recognized Expert Contributor
sqrt does not take int arguments. You have to pass it a float or a double. Reference page. Change the type of x and y to float, or cast them to a float inside the function. Other than that, your code is fine.
Hope this helps.
Oct 4 '08 #2
Gangas
2 New Member
sqrt does not take int arguments. You have to pass it a float or a double. Reference page. Change the type of x and y to float, or cast them to a float inside the function. Other than that, your code is fine.
Hope this helps.

The problem is that i am trying to create program to finds all the Pythagorean triples up to 500 , So my function will return 1 if the square root is integer, i am not sure, but I dont think that i can change the type of x and y to float,
Oct 4 '08 #3
boxfish
469 Recognized Expert Contributor
There should not be a problem with changing the function's arguments to floats. You can call a function that takes an float argument with an int instead and the int will just be converted to a float. No harm done. I notice I'm contradicting myself because if what I just said was true, then you should have no problem calling sqrt with an int, but I think it doesn't let you do this with sqrt because it's an overloaded function and so your compiler doesn't know which type to convert to. But anyway, you can do this,
Expand|Select|Wrap|Line Numbers
1. int square_check (float x, float y )
2. {
3. // Function Body Snipped.
4. }
or if you have a good reason to make the function's arguments ints, you can cast x+y to a float like this.
Expand|Select|Wrap|Line Numbers
1. int square_check (int x, int y )
2. {
3. float z= sqrt((float)(x+y));
4.
5. if (z ==(int)z)
6. return 1;
7. else
8. return 0;
9.
10. }
Hope this helps, and when you are posting code, please put [CODE] before it and [/CODE] after it, to make it show up in a code box.
Oct 4 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
There is a function in math.h called fmod(double x, double y) that calculates the remainder of two floating point numbers based on x/y.

If x is your square root then x/1.0 will have a remainder of zero when x has a zero decimal portion. That is, x is an int.

You should not have to typecast.
Oct 4 '08 #5
newb16
687 Contributor
What if e.g. sqrt(4) returns 3.9(9) ? I'd round it to int and check root's square equals x as integer, like
Expand|Select|Wrap|Line Numbers
1. int x;
2. ...
3. int root = (int)(floor(sqrt(x)+0.5));
4. if ( root*root == x ) ...
5.
Oct 4 '08 #6
boxfish
469 Recognized Expert Contributor
I think what weaknessforcats is suggesting is to replace
if (x == (int)x)
with
if (fmod(x, 1.0f) == 0)
The code you have provided looks needlessly complex.
Oct 4 '08 #7
donbock
2,426 Recognized Expert Top Contributor
I have to agree with newb16 in post #6. Comparing two integer values insures that you're not misled by the intrinsic inaccuracies present in all floating-point math. You might be able to write a portable floating-point comparison that works -- but why work that hard?
Oct 6 '08 #8