# Why does this sqrt function only work for perfect squares?

Page 1 of 1

## 5 Replies - 703 Views - Last Post: 17 July 2015 - 06:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=378918&amp;s=bc2eb7fbe757af11596e393ec45f94db&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Nathanael

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 12324
• 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).

### #3 general656

• D.I.C Regular

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

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

Posted 17 July 2015 - 04:37 PM

Nathanael, 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

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12324
• 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.

### #5 Nathanael

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

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

Posted 17 July 2015 - 04:49 PM

general656, 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.

macosxnerd101, 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.)

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12324
• 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.