# Allowing negative square root

• 11-10-2010
jae5086
Allowing negative square root
I don't have a clue how to "break" this to actually return a negative square, but that is what I need to figure out. For example, the sqrt of -100 should be -10, but I just get the error telling me that's silly (-1#.IND).

Code:

```#include <iostream> #include <cmath> using namespace std; double wierdsquareroot(double x) {         return sqrt(x); } int main ()  // Program calculates the square root of a number {         double num;         do         {                 cout << "Enter a number (a double): "; cin >> num;                 cout << "The square root is " << wierdsquareroot(num) << endl;         }while (num != 0); }```
• 11-10-2010
laserlight
If the number is negative, change it to its absolute value and then apply the square root. However, you also need some way to inform the caller that the result is imaginary.
• 11-10-2010
KCfromNC
Something like this?
Code:

```if (x < 0.0)   return -sqrt(-x); return sqrt(x);```
• 11-10-2010
laserlight
Yes, if that is what you want.
• 11-10-2010
brewbuck
Quote:

Originally Posted by jae5086
For example, the sqrt of -100 should be -10

In what screwed-up universe is -10 * -10 == -100?
• 11-10-2010
ಠ_ಠ
Quote:

Originally Posted by brewbuck
In what screwed-up universe is -10 * -10 == -100?

the universe where every '-' is replaced by a '+'