5 Replies - 703 Views - Last Post: 17 July 2015 - 06:28 PM Rate Topic: -----

#1 Nathanael   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-July 15

Why does this sqrt function only work for perfect squares?

Posted 17 July 2015 - 04:18 PM

I tried making a function to find the square root of a number, but it is only working for perfect squares (otherwise the code seems to never finish). Here is the code:

double sqrt(double a) {
    if (a<0) {
        cout << "\nsqrt(" << a << ") is imaginary\n";
        return -1;
    }
    int x = a/2; // first guess (could be better)
    while (x*x-a != 0){
        x = 0.5*x+a/(2*x); // next guess, by Newton's method
    }
    return x;
}



I also tried making the while-loop-test (x*x-a>0.01 || x*x-a<-0.01) but it still only worked for perfect squares. (Oddly enough, though, it gave the exact answer for perfect squares. I expected it to limit the accuracy.)

Is This A Good Question/Topic? 0
  • +

Replies To: Why does this sqrt function only work for perfect squares?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Why does this sqrt function only work for perfect squares?

Posted 17 July 2015 - 04:31 PM

See your condition: while (x*x-a != 0){. Note that many square roots, such as sqrt(2), are irrational. So you can't have a perfect numerical representation of them. Rather than checking if they're equal to 0, check if x * x - a < epsilon, for a pre-determined epsilon value (where epsilon denotes the margin of error).
Was This Post Helpful? 0
  • +
  • -

#3 general656   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 288
  • Joined: 25-March 15

Re: Why does this sqrt function only work for perfect squares?

Posted 17 July 2015 - 04:37 PM

View PostNathanael, on 17 July 2015 - 04:18 PM, said:

I tried making a function to find the square root of a number, but it is only working for perfect squares (otherwise the code seems to never finish). Here is the code:

double sqrt(double a) {
    if (a<0) {
        cout << "\nsqrt(" << a << ") is imaginary\n";
        return -1;
    }
    int x = a/2; // first guess (could be better)
    while (x*x-a != 0){
        x = 0.5*x+a/(2*x); // next guess, by Newton's method
    }
    return x;
}



I also tried making the while-loop-test (x*x-a>0.01 || x*x-a<-0.01) but it still only worked for perfect squares. (Oddly enough, though, it gave the exact answer for perfect squares. I expected it to limit the accuracy.)


Yeah ... try making double x instead of int x ;
When you deal with doubles in a function and you don't want to deal with floor... deal only with doubles and floats ...

This post has been edited by general656: 17 July 2015 - 04:38 PM

Was This Post Helpful? 2
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Why does this sqrt function only work for perfect squares?

Posted 17 July 2015 - 04:40 PM

Good catch, general656! I totally missed that. :)
Was This Post Helpful? 0
  • +
  • -

#5 Nathanael   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-July 15

Re: Why does this sqrt function only work for perfect squares?

Posted 17 July 2015 - 04:49 PM

View Postgeneral656, on 17 July 2015 - 04:37 PM, said:

Yeah ... try making double x instead of int x ;
When you deal with doubles in a function and you don't want to deal with floor... deal only with doubles and floats ...

Of course! Thank you. Sorry for the simple mistake, I'm very new to programming.


View Postmacosxnerd101, on 17 July 2015 - 04:31 PM, said:

See your condition: while (x*x-a != 0){. Note that many square roots, such as sqrt(2), are irrational. So you can't have a perfect numerical representation of them. Rather than checking if they're equal to 0, check if x * x - a < epsilon, for a pre-determined epsilon value (where epsilon denotes the margin of error).

As I said in the OP, I already tried that idea ... "while (x*x-a >0.01 || x*x-a < -0.01)"

I just want to say that with general656's correction, the code works with "while (x*x-a != 0)"

I'm assuming it works because eventually the error x*x-a is below the accuracy of what the computer stores. (But I like I said I'm new, so I don't quite know what I'm talking about. I just wanted to mention (x*x-a != 0) works and gives the best accuracy.)
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: Why does this sqrt function only work for perfect squares?

Posted 17 July 2015 - 06:28 PM

Quote

As I said in the OP, I already tried that idea ... "while (x*x-a >0.01 || x*x-a < -0.01)"

I do apologize for missing that in your original post. :)

You're likely to get a good approximation with fewer iterations if you use an epsilon value. The cmath library has a sqrt() function. You can test against that for accuracy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1