472,107 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,107 software developers and data experts.

Checking if the square root result is integer

2
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 13900
boxfish
469 Expert 256MB
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
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 Expert 256MB
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 Expert Mod 8TB
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 512MB
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 Expert 256MB
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,425 Expert 2GB
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by cplusplus | last post: by
15 posts views Thread by Stig Brautaset | last post: by
99 posts views Thread by Mikhail Teterin | last post: by
32 posts views Thread by priyam.trivedi | last post: by
4 posts views Thread by sathyashrayan | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.