Thread: Calculating Square Root?

  1. #1
    Internet Superhero
    Join Date
    Sep 2006

    Calculating Square Root?

    I've been messing about with some programs to calculate some basic math for me, but i've hit a brick wall with Square Root. Obviously, i could just use the sqrt(); from cmath, but i want to try and do it manually using some kind of formula or method rather than just using a premade function.

    I've been browsing around for a formula for computing sqrt, and Wikipedia has a couple.

    They're kind of complicated and i have a hard time understanding most of them. I did try the one with the logarithm, sqrt(x) = 10.5^log(x), but that gave me mostly random results, and definitely not the sqrt.

    What i have now is not the prettiest way of doing it. This program actually succeeds in trying every combination of digits until it hits the correct one, but it only works with integers, so if the input to the program isn't something like 9 or 16, it just keeps on calculating numbers forever.

    #include <iostream>
    #include "clscr.h"
    float Square(float a);
    int main()
        float a = 0, b = 0;
        std::cout << "Square Root (a)" << std::endl;
        std::cout << "Enter a: ";
        std::cin >> a;
        b = Square(a);
        std::cout << "Square Root (" << a << ") = " << b << std::endl;
    float Square(float a)
          int i = 0;
          float b = 1;
          while (1)
                if ((b*b) == a)
    A very ugly way of doing it - i know. Is there a simple formula i have missed? How does the sqrt(); in cmath do it? I need it to work with non-integers aswell. As you might have noticed, im not very good at maths and long formulas, please be gentle

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    There was a recent contest on computing square roots with restriction on division. Without (and with) this restriction one idea is to implement the Newton-Raphson method.
    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
    Sep 2006

    The Babylonian method (essentially Newton's method for square roots) is probably the one you should be using. There's not much point in using logs to compute square roots since log() is more complicated to compute than sqrt(), so if you're not willing to call sqrt() then it doesn't make sense to call log() either.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Quote Originally Posted by Neo1 View Post
    I did try the one with the logarithm, sqrt(x) = 10.5^log(x), but that gave me mostly random results, and definitely not the sqrt.
    Well, the way you've written it here is wrong. It is not 10.5 raised to the power of log(x) but rather 10 raised to the power of (.5 times log(x)). Additionally, what log function did you use? The formula works off of base 10 log which would be the log10 function and not the other log function. If you just used the log function then you used the wrong one.

    In code and written as a function this would be:
    double SquareRoot(double val)
        return pow(10.0,0.5*log10(val));  // 10 raised to the power (0.5 * log10(val))
    Results from this match the return value of the sqrt function in my test program.
    Last edited by hk_mp5kpdw; 05-24-2007 at 02:14 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Join Date
    Aug 2002
    Tucson, Arizona
    Sorry if I am crazy, but can't you just raise the value to 1/2(.5)

    #include <iostream>
    #include <cmath>
    const double VICINITY = 0.0001;
    double SquareRoot(double val)
    	return pow(val, .5);
    int main()
    	//Grab the square roots
    	double customRoot = SquareRoot(10.0);
    	double libRoot = sqrt(10.0);	
    	//Calc the difference between the two values
    	double valueDifference = fabs(libRoot - customRoot);
    	//Used to work around floating point
    	if(valueDifference < VICINITY){
    		std::cout<<"The Roots Match";
    		std::cout<<"The Roots Are Different, By This Value: "<<valueDifference;
    	//Wait for user input
    	return 0;
    Last edited by prog-bman; 05-24-2007 at 03:33 PM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Basically you do this:
    float SquareRoot(float a)
        float c, b = a;
        do {
            c = b;
            b = (a/b + b) * 0.5f;
        } while (fabs(c - b) < EPSILON);
        return c;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

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