432,188 Members | 844 Online
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

# from cartesian to polar

 P: 18 Hi! im trying to build a program that converts cartesian to polar everthing seems to work fine except the atan2(y,x) func which does not culc the right value !! my code is ; Expand|Select|Wrap|Line Numbers #pragma warning(disable:4996) #include #include #include   void kartesiska(double x, double y, double *o, double *p);   void main(void){    double a, b;  a = 7;  b = 5;    kartesiska(a, b, &a, &b);    if (a>0)   printf("the value is %f\n%f\n", a, b);      system("pause"); } void kartesiska(double x, double y, double *o, double *p){     if (x > 0)     {         *o = sqrt((x*x) + (y*y));         *p = atan2(x,y);     }       else         printf("undef!\n");     return 0; } [/code] Nov 13 '15 #1
5 Replies

 Expert Mod 5K+ P: 9,197 What do you mean that atan2 "does not calc the right value"? Do you have an example of the correct value? The code posted will not compile because the kartesiska function has a void return type yet there is a return 0 in the code. What compiler are you using? Also, the code mixes integer and floating point. The rule is to use either all integer or all floating point. Values like 4 should be 4.0. Nov 13 '15 #2

 Expert 100+ P: 2,398 In your opening paragraph you correctly say atan2(y,x), however line 26 of your program says atan2(x,y). It isn't easy to keep the atan2 arguments straight; for example:For C and C++ it is atan2(y,x) For Excel it is ATAN2(x,y) For perl it is atan2 y x For Mathematica it is ArcTan[x,y] By the way, line 8 defines main to return void. That's wrong, it should return int. Why does line 23 block all negative X values? There is nothing wrong with the 2nd and 3rd quadrants. What is not defined for atan2 is when both arguments are zero. However, you should never compare a floating point value for equality (==). Nov 13 '15 #3

 P: 18 how to compare the coz no matter how i do it prints out both undef and the valus as if my if statment does not effect at all !! Expand|Select|Wrap|Line Numbers if (x != 0){         *o = sqrt((x*x) + (y*y));         *r = atan2(y, x) * 180 / 3.14159265359; // converting from radians to degrees !     }     else {           printf("undef\n");     } Nov 13 '15 #4

 Expert 100+ P: 2,398 You print "undef" inside kartesiska() if the x argument is equal to 0. You print the polar value if a is greater than 0 after returning from kartesiska(). You would think that if the first comparison found x==0 that the second comparison would have to find !(a>0), but perhaps inconsistent paths are being taken. How could that happen? I warned you earlier that it is unwise to compare floating point values for equality (or not-equality). Refer to What Every Computer Scientist Should Know About Floating-Point Arithmetic for a detailed explanation. Another possibility is that maybe your code does not look exactly like the posted snippet. For example, a spurious semicolon in kartesiska() between else and { or in main() between if and { would cause the behavior you describe. Some other issues:It is perfectly acceptable for x to be 0. The real issue is that you shouldn't call atan2() if vector length *o is 0. However, *o is a floating point number so you should not merely compare it to 0. If you do compute the vector length before deciding whether to do the conversion, then that decision should be based on a variable local to kartesiska() rather than immediately writing the length to *o. If you tell the caller there was no conversion, then you ought not to overwrite any of the caller's variables. You are performing two independent comparisons to determine if kartesiska() has performed a conversion. That provides an opportunity for inconsistency. Better for kartesiska() to have a return value that informs the caller if a conversion occurred. It is good practice to test pointers before dereferencing them to make sure they are not NULL. If the vector length is zero then it might be more correct to return an appropriate hard-coded polar value, such as (0,0), without calling atan2(). In that case, a conversion always takes place and there is no need for conditional logic in the caller. Nov 13 '15 #5

 P: 18 thank you very much for ur time ! i will do my best to fix it now ! Nov 13 '15 #6