0

Im trying to make a simple converter using reference to convert between cartesian and polar the problem is it gives me wrong answers and sometimes 0 ,0 . I want to know what's the problem and how can i fix it . this is the code :

#include <iostream>
#include <cmath>
using namespace std;
void cartesianToPolar(int x,int y,float &r,float &q ) {
r = sqrt(x * x + y * y); q = atan(y / x);
}
void polarToCartesian(float r, float q, int &x, int &y) {
    x = r * cos(q); y = r * sin(q);
}
int main() {
    int cevap ;
    int  x = 0 , y = 0 ,xx = 0 , yy = 0;
    float r = 0 , q = 0 , rr = 0 , qq = 0 ;
    cout << "Choose please....." << endl;
    cout << "1-Cartesian -> polar "<<endl;
    cout << "2-polar ->Cartesian " << endl;
    cin >> cevap;
    if(cevap==1){
    cout << "enter x value: " ;
    cin >> x;
    cout << "enter y value: " ;
    cin >> y;
  
    cartesianToPolar(x,y,rr,qq);
    cout << "r:  " << rr << "        " << "Q: " << qq << endl;
    }
    else if (cevap==2)
    {
        cout << "enter r value : ";
        cin >> rr;
        cout << "enter Q value: ";
        cin >> qq;
        polarToCartesian(r, q, xx, yy);

        cout << "x: " << xx << "        " << "y: " << yy << endl;
    }
    return 0;
}
MrXQ
  • 465
  • 8
  • 25
  • 1
    Please show sample in and output for good and bad cases. Explain what seems wrong about the bad cases. – Yunnosch Sep 03 '20 at 10:45
  • 1
    Why are you alternating between int and float? There are risks https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Yunnosch Sep 03 '20 at 10:45
  • @Yunnosch there is no good cases i always get wrong answers like this : enter x value: 5 enter y value: 5 r: 7.07107 Q: 0.785398 . I'm learning c++ i didn't know that its bad to use float and int together – MrXQ Sep 03 '20 at 10:48
  • 2
    Note that you should use `atan2(y, x)` and not `atan(y/x)`, to handle the case `x = 0` – Damien Sep 03 '20 at 10:50
  • 1
    The values you give for 5,5, are exactly what I expect as polar coordinates. Please explain what is wrong with them. – Yunnosch Sep 03 '20 at 10:59
  • @Yunnosch yes, you are right , converting from cartesian to poral works fine the problem was at the poral > cartesian part . – MrXQ Sep 03 '20 at 11:01
  • And you won't provide sample input and output with explanation of what makes the output wrong. Why? – Yunnosch Sep 03 '20 at 11:03

1 Answers1

2

Results of both of your functions should be floats, not ints:

void cartesianToPolar(float x, float y, float &r, float &q ) {
    r = sqrt(x * x + y * y); q = atan(y / x);
}

void polarToCartesian(float r, float q, float &x, float &y) {
    x = r * cos(q); y = r * sin(q);
}

The values you were computing were correct, but the result was converted to integers afterwards. Conversion to int happens by trunctation, that is, 0.1 and 0.9 all become just 0.

You also had a typo in your polar to cartesian conversion in the main. Used the wrong variable. Correct is:

polarToCartesian(rr, qq, xx, yy);

Following @Yunnosch comment, you should use atan2() rather than atan(). Detailed explanation can be found here

atru
  • 4,699
  • 2
  • 18
  • 19
  • 2
    Seriously.Use atan2() instead of atan(), or woe. – Yunnosch Sep 03 '20 at 11:00
  • 1
    @Yunnosch - added to the post, haven't really used them in C++ or C so was unaware. – atru Sep 03 '20 at 11:05
  • 1
    @MrXQ - gladly! – atru Sep 03 '20 at 11:06
  • It is fine. I should dial down on the "woe". It is just so amazingly more convenient, because of being made especially for this use case. – Yunnosch Sep 03 '20 at 11:06
  • 1
    It could be a deliberate design decision to have the Cartesian coordinates integer only. The fix would be a cast `sqrt(double(x * x + y * y)), atan2((double)y, (double)x);` –  Sep 03 '20 at 12:45