Help with C++ program execution.

//Find the root of the equation (x^2)-2 by bisection method.

#include<iostream>
using namespace std;
double a,x;
double f(double x)
{
    return ((x*x)-2);
}                                         //Suppose the function is (x*x)-2.
void calcx(double a1,double b1)
    {
        x = (a1+b1)/2;
        while((f(x)*f(a))!=0)                //If f(x)*f(a)=0, then the root is 'x' itself!
        {
            if(f(x)*f(a) < 0)
            {
                if(x==1.41421509)                               //The answer should be 1.414... so given like this!
                {
                    cout<<"Process completed!";
                    cout<<"Result is: "<<x;
                }
                a1=x;                                       //The value 'a' is replaced by x and 'b' remains the same.
                calcx(a1,b1);                               //Then, we'll do recurrsion for range (x,b)
            }
            else if(f(x)*f(a) > 0)
            {
                if(x==1.41421509)
                {
                    cout<<"Process completed!";
                    cout<<"Result is: "<<x;
                }
                b1=x;
                calcx(a1,b1);                               //Here, recurrsion is done for range (a,x).
            }
        }
    }
int main()
{
    double b,res;                               
    cout<<"Enter the values of 'a' and 'b': ";
    cin>>a>>b;                                            //The range already given.
    calcx(a,b);
    return 0;
}

OUTPUT:

Segmentation fault

Q. Can i know why this kind of message is being displayed?

Your program will recurse forever, consuming hypothetically infinite amounts of memory. "if(x==1.41421509)" will probably never return true, because floating point values don't have infinite precision. You should do

if(fabs(x-1.41421509) < .001)

instead, to see if it's approaching a value precise enough. Adjust ".001" to taste.

Since your computer probably won't let you have infinite amounts of stack space, recursing forever causes it to crash.

Why not use a loop instead of having calcx() call itself?

do
{
    // Calculate result
    // Set new ranges
    // Only if not precise enough, loop through again
} while(fabs(x-1.41421509) >= 0.001);

---------- Post updated at 10:53 AM ---------- Previous update was at 10:50 AM ----------

You should probably also have a count value, so if it gets stuck, it'll give up after a few thousand loops.

I think that's probably one of the most misunderstood things about floats. Using the equals operator on them is generally a bad idea. As Corona pointed out, you should test within a range of the value you want. The easiest way to do that is how he showed you, where, in his example the .001 would be the precision. The smaller that number (.000001 for example) the "closer" to your target you are. If you use, for example, .1, then you could be further from your target.

Of course, as with anything, this could still cause an issue. I mean, if you increase/decrease the value too much at each iteration you may miss your "window" of precision, never to return. So, you need to be aware and compare within an appropriate precision for the algorithm you're running.