N-TH root of number in C++

-1

I'm trying to calculate the n-root of a number in c++ using pow, so I tried to do as follow

#include <cmath.h>
...
double n_root_of_a = pow(a,1.0/n);
...

but this isn't working it gives me the following execution error "Process returned -1073741571 (0xC00000FD)". but if I run the same code in an online compiler It's works fine. I'm using Codeblocks with MINGW on windows and don't know what is happening.

EDIT: full code

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <cmath>
#include <cctype>

const char ADD = '+';
const char SUB = '-';
const char MUL = '*';
const char DIV = '/';
const char POW = '^';
const char ROOT = '~';

using namespace std;

template<typename elem>
ostream& operator<<(ostream& os, const vector<elem>& vec)
{
    typename vector<elem>::const_iterator it = vec.begin();
    os << "{";
    while(it != vec.end())
    {
        os << *it;
        it++;
        if(it == vec.end())
            os << "}";
        else
            os << ", ";
    }
    return os;
}

double add(double op1, double op2)
{
    return op1 + op2;
}

double sub(double op1, double op2)
{
    return op1 - op2;
}

double mul(double op1, double op2)
{
    return op1 * op2;
}

double div(double op1, double op2)
{
    return op1 / op2;
}

double pow(double op1, double op2)
{
    return pow(op1,op2);
}

double root(double op1, double op2)
{
    cout << op1 << endl;
    cout << op2 << endl;
    cout << 1.0/op2 << endl;
    cout << pow(4.0,0.5) << endl;
    return pow(op1,1.0/op2);
}



bool calculate(vector<char> rpn, double& res)
{
    vector<char>::iterator it = rpn.begin();
    stack<double> operands;
    bool error = false;
    while(it != rpn.end() && !error)
    {
        cout << "while with it: " << *it << endl;
        switch(*it)
        {
        case ADD:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = add(op1,op2);
            cout << "add with op1: " << op1 << ", op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case SUB:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = sub(op1,op2);
            cout << "sub with op1: " << op1 << ", op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case MUL:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = mul(op1,op2);
            cout << "mul with op1: " << op1 << ", op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case DIV:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = div(op1,op2);
            cout << "div with op1: " << op1 << ", op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case POW:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = pow(op1,op2);
            cout << "pow with op1: " << op1 << ", op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case ROOT:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = root(op1,op2);
            cout << "root with op1: " << op1 << ", op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        default:
            {
            cout << "entra al default" << endl;
            int op;
            if (isdigit(*it))
                op = (int) *it - '0';
            else
                op = (int) *it - 'a' + 10;
            operands.push(op);
            break;
            }
        }
        it++;
    }
    if(operands.size() == 1){
        res = operands.top();
        return true;
    }
    return false;
}

char inttochar(int a) {
    if (a >= 0 && a <= 9)
        return '0' + a;
    else
        return 'a' + a - 10;
}


int main(int argc, char *argv[])
{
    cout << "Hello world!" << endl;
    double xd = pow(4.0,0.5); // <-------------- here it's chrashes
    cout << xd << endl;
    vector<char> operators{ADD,SUB,MUL,DIV,POW,ROOT};
    vector<int> numbers(0);
    for(int i=1; i<argc; i++)
    {
        numbers.push_back(atoi(argv[i]));
    }
    cout << operators << endl;
    cout << numbers << endl;

    vector<char> rpn{'4','2',ROOT};
    double result = -20.0;
    if(calculate(rpn,result))
        cout << result << endl;

    vector<char> aux(0);
    /*for(int i=0; i<7; i++) {
        for()
    }*/
    return 0;
}
c++
mingw
pow
asked on Stack Overflow Dec 29, 2020 by Mattii • edited Dec 29, 2020 by Mattii

1 Answer

4

The error is in your redefinition of pow at line 54, that calls itself:

double pow(double op1, double op2)
{
    return pow(op1,op2);
}

This is an infinite recursive function. You can fix by calling std::pow in your overloaded function. Or simply remove the overload completely, as it provides no value.

answered on Stack Overflow Dec 29, 2020 by prapin

User contributions licensed under CC BY-SA 3.0