473,542 Members | 16,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

4
8673
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied together and the square root of the product is returned and displayed for the user. The function should return a double. Hint: If you multiply an...
15
4134
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: struct node { struct node *next; void *data; };
99
5048
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
2
3126
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get a pseudorandom number generator. So, rather than calculate the square root to try to get a particular precision answer, you would calculate it to x...
32
4948
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor Series? Is there any other way of doing it rather than using series? Thank you, Priyam
4
8448
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a simple task but my mind suddenly got stuck. I am trying to implement a square root function as a practice. I am able to code for the perfect square
10
13618
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever i go to print out the answer its rounding instead of giving me the answer i need... any help?? double num1, num2, the_root; cout<<...
4
8076
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code involving lot of matrices. At one point I need to calculate the square root of a matrix e.g. A which contains non-zero off-diagonal elements. I...
5
3122
by: WanHongbin | last post by:
#include <stdio.h> double square(); /*without declare main() { double s; s = square(2); printf("%g\n", s); }
0
7322
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7665
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5239
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3377
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1790
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.