1

I know that by changing the trig function names I get rid of segfault. However, it is not completely clear to me why. My guess is that I get recursive calls inside my implementations. Can someone please give a rigorous explanation here?

Furthermore, I'm willing to keep things this way thus am wondering what should be added/modified to leave names of my functions as they are.

P.S.: I know it's a dumb question, just starting c++ here, so bear with me, please. :)

Code:

#include <iostream>
#include <string>
#include <cmath>

#define _USE_MATH_DEFINES

using namespace std;

/*
   Represents a calculator with various functions.
*/
class Calc {
public:
    //arithmetic functions
    static double sum(double a, double b){ return a+b; };
    static double subtract(double a, double b){ return a-b;};
    static double multiply(double a, double b){ return a*b; };
    static double divide(double a, double b){ if (b == 0) return b; return a/b; };
    static double avg(double a, double b){ return (a+b)/2; };

    //trigonometric funcitons
    static double sin(double x){ return sin(M_PI*x); };
    static double cos(double x){ return cos(M_PI*x); };
    static double tan(double x){ return tan(M_PI*x); };
};

void getValue(double *a){
    cout << "Please, enter a value: " << endl;
        cin >> (*a);
}

void getValues(double *a, double *b){
    cout << "Please, enter two values: " << endl;
        cin >> (*a) >> (*b);
}

bool getCommand(string *cmd){
    cout << "Please, enter a command (+,-,*,/,avg, sin, cos, tan, exit): ";
    cin >> (*cmd);
    if ( (*cmd) == "exit"){
        return false;
    } else {
        return true;
    }
}

int main (){
    string cmd;
    double a,b;

    while (getCommand(&cmd)){
        if (cmd == "sin" || cmd == "cos" || cmd == "tan"){
            getValue(&a);
            if (cmd == "sin"){
                cout << "Sine: " << Calc::sin(a) << endl;
            } else if (cmd == "cos"){
                cout << "Cosine: " << Calc::cos(a) << endl;
            } else if (cmd == "tan"){
                cout << "Tangent: " << Calc::tan(a) << endl;
            }
        } else {
            getValues(&a,&b);
            if (cmd == "+"){
                cout << "Summation: " << Calc::sum(a,b) << endl;
            } else if (cmd == "-"){
                cout << "Subtracttion: " << Calc::subtract(a,b) << endl;
            } else if (cmd == "*"){
                cout << "Multiplication: " << Calc::multiply(a,b) << endl;
            } else if (cmd == "/"){
                cout << "Division: " << Calc::divide(a,b) << endl;
            } else if (cmd == "avg"){
                cout << "Average: " << Calc::avg(a,b) << endl;
            }
        }
    }
    return 0;
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
Denys S.
  • 6,358
  • 12
  • 43
  • 65

2 Answers2

7

You got it, recursive calls. I think you want

static double sin(double x){ return ::sin(M_PI*x); };

Using the scope resolution operator :: will make your sin function call the global sin function, instead of recursively calling yours.

BTW if you are trying to convert degrees to radians then you actually need

static double sin(double x){ return ::sin((M_PI/180.0)*x); };

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    Or better yet, throw out the `using`-directive and write `std::sin`. – Björn Pollex Nov 15 '12 at 08:08
  • Ok, this M_PI note is nice, what's the best option then? Pre-calculation using atan? – Denys S. Nov 15 '12 at 08:10
  • 2
    @den-javamaniac: you can just [define it conditionally](http://stackoverflow.com/questions/5007925/using-m-pi-with-c89-standard). maybe john will put the comment about it back, when he has had time to think. hopefully. but until then, I'd better mention here in the comment that **`M_PI` is non-standard**, but a common extension. – Cheers and hth. - Alf Nov 15 '12 at 08:15
  • @Cheersandhth.-Alf I've had people edit my answers a couple of times recently. Not to correct them but to 'improve' them. I don't like it. Also in doing so you squashed an edit I was making myself. A comment such as Björn made seems a better way to suggest corrections or improvements. – john Nov 15 '12 at 08:18
  • @john: this is (meant to be) a collaborative site. so I would just be thankful if people improved my postings. not happened yet, though... anyway, bjørn's comment is one about style and personal preference, which therefore would be absolutely wrong to put into someone else's answer. the information about `M_PI` is about crucial substance, defined by international standards. – Cheers and hth. - Alf Nov 15 '12 at 08:22
1

1) You should #define _USE_MATH_DEFINES before you #include <cmath>.

2) Your Calc functions indeed contain infinite recursion (they're calling themselves). You need to change them as follows:

static double sin(double x){ return std::sin(M_PI*x); }

That is, qualify the call to sin() to make it clear you want to call the global one.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455