473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 14411
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,426 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

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

Similar topics

4
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...
15
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: ...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
2
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...
32
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...
4
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...
10
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...
4
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...
5
by: WanHongbin | last post by:
#include <stdio.h> double square(); /*without declare main() { double s; s = square(2); printf("%g\n", s); }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.