Allowing negative square root

This is a discussion on Allowing negative square root within the C++ Programming forums, part of the General Programming Boards category; I don't have a clue how to "break" this to actually return a negative square, but that is what I ...

  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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    Yes, if that is what you want.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    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, 08:10 AM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21