Checking if the square root result is integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangas
    New Member
    • Oct 2008
    • 2

    Checking if the square root result is integer

    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
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #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.

    Comment

    • Gangas
      New Member
      • Oct 2008
      • 2

      #3
      Originally posted by boxfish
      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,

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        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,
        Code:
        int square_check (float x, float y )
        {
        // Function Body Snipped.
        }
        or if you have a good reason to make the function's arguments ints, you can cast x+y to a float like this.
        Code:
        int square_check (int x, int y )
        {
        float z= sqrt((float)(x+y));
        
        if (z ==(int)z)
        return 1; 
        else 
        return 0;
        
        }
        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.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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.

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            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
            Code:
            int x;
            ...
            int root = (int)(floor(sqrt(x)+0.5));
            if ( root*root == x ) ...

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              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.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                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?

                Comment

                Working...