Wierd Square Root

Hey guys. I'm currently working on a project that will ask for a number, and by using a function, will output the square root of the number. The catch is that:
1. If the user enters a negative number, the output will yield the number as if it was a positive, only with a negative.
i.e) -sqrt(4) = -2
Mine outputs -1.#IND

Also, if the user enters a 0, the loop will stop processing. Mine keeps going through the loop and for some reason ignoring the while statement at the end.

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>

using namespace std;

double weirdSquareRoot(double number)
{
	double result = sqrt(number);
	return result;
}
int main()
{
	double value;
	do
	{
	cout << "Enter a number (a double): ";
	cin >> value;
	cin.ignore();
	double answer = weirdSquareRoot(value);
	cout << "The square root is " << answer << endl;

	} while (value != '0');

	
	

	
}


I appreciate any help :)
It should be while (value != 0);. Note that 0 is not the same as '0'.

Also, your weirdSquareRoot doesn't do what you want it to do... I suggest an implementation like this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double weirdSquareRoot(double number)
{
    double result;
    
    if (/*number is negative*/)
    {
        /*result = - square_root_of ( - number)*/
    }
    else
    {
        /*result = square_root_of (number)*/
    }
    
    return result;
}

A short function might be:

double weirdSquareRoot(double number){return sqrt(abs(number)) * abs(number)/number;}

Not sure how that might do on efficiency, though.

Alternatively, you could steal the sign from 'number' using bit logic, which would probably be faster than '*abs(number)/number'.
Last edited on
Thanks for the input.

I changed my code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>

using namespace std;

double weirdSquareRoot(double number)
{
	double result;

	if (number < 0)
	{
		result = -(sqrt(number));
	}
	else
	{
		result = sqrt(number);
	}
	return result;
}
int main()
{
	double value;
	do
	{
	cout << "Enter a number (a double): ";
	cin >> value;
	cin.ignore();
	double answer = weirdSquareRoot(value);
	cout << "The square root is " << answer << endl;

	} while (value != 0);

	
	

	
}

Instead now, it's giving me an output of "1.#QNAN"
In math, when you take the square root of a negative number, you get what's called an "imaginary number". For instance, the square root of -9 is 3i, where "i" is the square root of negative one.

In your code, you took the square root of a negative number when you used "sqrt(number)" for the case in which "number" was negative. That's why you got the weird output - the computer was trying to tell you that you got an imaginary number back.

You can fix it by changing that part of the code to "sqrt(-number)", to flip the negative back to a positive.
Last edited on
You forgot something here -> result = -(sqrt(number));.
Woo, that makes sense. I have it now! Thanks for all of your help :)
Topic archived. No new replies allowed.