Quantcast

Jump to content


Photo

[C++]SQRT within the cmath library


  • Please log in to reply
3 replies to this topic

#1 Supernature

Supernature
  • 87 posts

Posted 19 July 2010 - 02:25 PM

Since I seem to be getting more help here than from my actual instructor, I've encountered another blunder. (I really hate that I have a new project due EVERYDAY. Dumb summer classes)

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:
#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 :)

#2 aweroij

aweroij
  • 44 posts

Posted 19 July 2010 - 02:38 PM

Since I seem to be getting more help here than from my actual instructor, I've encountered another blunder. (I really hate that I have a new project due EVERYDAY. Dumb summer classes)

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:

#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 :)


#include <iostream>
#include <cmath>

using namespace std;

double weirdSquareRoot(double number)
{
	// if the number is negative
	if (number < 0)
		// return the negative of square root of absolute value of number
		return -sqrt(-number);
	else
		// else return the square root of the number
		return sqrt(number);
}

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;

	// '0' (as opposed to just 0) is an ASCII character code
	} while (value != 0);

	// need a return statement
	return 0;
}


#3 Supernature

Supernature
  • 87 posts

Posted 19 July 2010 - 05:50 PM

Oh, gotcha! Thansk man!!

#4 Andy

Andy
  • 226 posts

Posted 24 July 2010 - 10:16 PM

sign(value)*sqrt(abs(value));
abs() turns a negative number into a positive one
sign() returns either 1 or -1 depending on the sign of the value given to it.

Edited by Andy, 24 July 2010 - 10:17 PM.



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users