Thread: Allowing negative square root

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    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);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Something like this?
    Code:
    if (x < 0.0)
       return -sqrt(-x);
    return sqrt(x);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, if that is what you want.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jae5086 View Post
    For example, the sqrt of -100 should be -10
    In what screwed-up universe is -10 * -10 == -100?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by brewbuck View Post
    In what screwed-up universe is -10 * -10 == -100?
    the universe where every '-' is replaced by a '+'
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Root Recursive Problem
    By lostandconfused in forum C Programming
    Replies: 3
    Last Post: 06-17-2010, 12:05 AM
  2. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM