Thread: Square roots / Powers etc.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    Square roots / Powers etc.

    I am an ultra newbie to all this, and I'm learnin in my own free time, so keep it simple plz. I know it is something to do with including <math.h>, which I can do, but then how do I use the functions within it? do I simply have to type num^3 or something and it will work, what about square roots?

    I have seen some code on this board for square roots but it looks to be in C, and I'd like C++ (I dont know anything about C so it is difficult for me to understand and learn from)

    Thanks for any help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <iostream.h>
    #include <math.h>
    
    int main ( ) {
        double a, b, c;
        a = 7.0;
        b = pow( a, 2 );
        c = sqrt( b );
        cout << a << " squared is " << b << endl;
        cout << c << " is the square root of " << b << endl;
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Thanks Salem I can now carry on with that maths program I've been working on, I get £5 from my maths teacher if I make a program that can solve linear, quadratic and cubic equations.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    47

    Equation solver

    There exist formulae which can solve quadratic and cubic equations. It is mathematically impossible for a simple formula to exist to solve quintic (not sure about quartic) polynomial equations.

    If the input is going to be like this, then it should be easy:

    If equation is: x^4 + 2x^3 - 9 = 0

    Then you input: 1, 2, 0, -9

    If you have to be able to solve more complicated ones, then you're in trouble! You'd have to write an equation parser, and then I guess you'd just have to graph it over some massive domain and find intersections with 0, using a binary search to narrow down on the exact X values of the intersections. Of course, that's ignoring imaginary roots. And if your roots are outside the domain, then you have another problem.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    Thanks to you too, TerranFury, I was under the impression that cubic equations could only be solved by trial and error, until I went out and looked for a formula. I had written a program for the trial and error method but it wasnt always reliable and was very slow. Now I can do it properly.

    I'm not intending to make a program to solve quartic and quintic equations just yet so I should be ok, I'm only just getting started. Win32API is my next step which I'm learning now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  3. Simplifing Square Roots
    By LiNeAr in forum C++ Programming
    Replies: 11
    Last Post: 10-01-2005, 08:56 PM
  4. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM
  5. Homemade Square Roots
    By MisterWonderful in forum C++ Programming
    Replies: 13
    Last Post: 03-03-2004, 09:21 PM