# Square Root of a number

This is a discussion on Square Root of a number within the C++ Programming forums, part of the General Programming Boards category; Because you are converting your double value to a float, so as soon as you get close enough to 3*3 ...

1. Because you are converting your double value to a float, so as soon as you get close enough to 3*3 is roughly 9, it will retrun 9-9 == 0 from your abs function [which by the way is a bad name, I'd call it absdiff or some such].

--
Mats

2. Actually he seems to be converting doubles to int's - even worse.

3. Originally Posted by anon
Actually he seems to be converting doubles to int's - even worse.
Yes, that's actually what I meant to type - somehow the brain-to-finger interface obviously is a bit faulty sometimes... ;-)

--
Mats

4. It's a bad idea to use the same names as standard library functions. There are functions named sqrt() and abs() in <cmath>. It was pointed out earlier, but it's worth restating that it's a VERY bad idea to use == or != with floating point numbers as it will produce inconsistent results.

5. still has warnings

6. What is your current code, and what are the current warnings?

7. Got it working

Code:
```#include <iostream>
using namespace std;

double absDiff(double a,double b)
{
if ((a-b) < 0)
{
return (a-b)*-1;
}
else
{
return (a-b);
}
}
double sqrtNum(double x)
{
double U = x;
double L = 0;
double guess = x/2;
if (x == 0 || x == 1)
{
return x;
}
else
while (absDiff((guess*guess),x) > .000000000001)
if (guess*guess > x)
{
U = guess;
guess = ((U - L)/2) + L;
}
else
{
L = guess;
guess = ((U - L)/2) + L;
}
return guess;
}
int main()
{
cout<<"Root ";
cout<<sqrt(5)<<endl;
}```
Any improvements needed?

8. You may want to do something special, in case someone decides to type sqrtNum(-1). (And notice, that you forgot to change your call in main to your function as opposed to the library function.)

9. Any improvements needed?
You could use abs() from <cmath> instead of implementing abdDiff(), but if your goal is to avoid using stuff from <cmath> entirely then change (a-b)*-1 to (b-a). You could also pull guess = ((U - L)/2) + L; to after the if and else blocks. Oh, and your indentation could be more consistent.

10. What do you mean by pulling guess = ((U - L)/2) + L?

11. Originally Posted by freddyvorhees
What do you mean by pulling guess = ((U - L)/2) + L?
You do it in the if-part; you do it in the else-part. So why not just do it, outside of the if statement? (Other than "I don't want to fiddle with all the braces".)

12. ahhhhhh

13. Maybe like this?
Code:
```	if (x == 0 || x == 1)
{
return x;
}
else
while (absDiff((guess*guess),x) > .0000001)
{
if (guess*guess > x)
U = guess;
else
L = guess;
}
guess = ((U - L)/2) + L;
return guess;```

14. Code:
`	if (x == 0 || x == 1)`
as stated several times, don't compare 'double' values with integer values. The condition is not correct.

15. Oh, and your initial guesses implicitly assume that 0 < sqrt(x) < x; this is not true if 0< x < 1. For instance, sqrt(0.25) = 0.5, and you won't find those numbers.

Page 2 of 3 First 123 Last
Popular pages Recent additions