The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help with number conversion
Discuss Help with number conversion in the C Programming forum on Dev Shed. Help with number conversion C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 5th, 2013, 05:19 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 39 m 42 sec
Reputation Power: 0
|
|
|
Help with number conversion
Hi guys. I'm trying to build a program that changes rectangular values (e.g; X + jY) Into a polar form (Length and angle). I've gotten this far with the code:
#include<stdio.h>
#include<math.h>
main()
{
int a, b, c, d;
float e, f;
printf("Enter first real number number\n");
scanf("%d",&a);
printf("Enter first imaginary number number\n");
scanf("%d",&b);
c = pow(b,2)+pow(a,2);
d = sqrt(c);
printf("Total = %d\n",d); /* this is the length in polar form */
e = atan(b/a);
f = e*(180/3.14159);
printf("%f\n",f); /*this is Angle in polar form*/
return 0;
}
I'm currently having 2 problems:
1. in instances where a > b , the output angle is always 0
2. my results appear to be the 'wrong way round' so to speak. For example, if a = 1 and b = 40, I attain a value of 88.5 on the program. However, on a calculator, the value is 1.5. These add to make 90, which suggests my results may be out of phase?
I was hoping someone here would have a better idea. I'm not a programmer forefront, and problems like this are really where my knowledge ends.
Thanks very much
|

January 5th, 2013, 06:43 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
Use double, not int, for your quantities. Even if you want to restrict your inputs to be integers (why?), you're going to run into trouble. For example, if a and b are ints, then b/a in C is integer division, which results in an int (basically, it's the true quotient truncated to an integer). sqrt(c) will be computed correctly (c will be promoted implicitly to double before the call), but obviously, if the result is not an integer, then assigning its value to the int variable d will not result in the correct value.
Also, atan2(y, x) is better than atan(y/x) here, since atan2 will give you an angle in the correct quadrant.
|

January 6th, 2013, 06:40 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 39 m 42 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Lux Perpetua Use double, not int, for your quantities. Even if you want to restrict your inputs to be integers (why?), you're going to run into trouble. For example, if a and b are ints, then b/a in C is integer division, which results in an int (basically, it's the true quotient truncated to an integer). sqrt(c) will be computed correctly (c will be promoted implicitly to double before the call), but obviously, if the result is not an integer, then assigning its value to the int variable d will not result in the correct value.
Also, atan2(y, x) is better than atan(y/x) here, since atan2 will give you an angle in the correct quadrant. |
Thanks for the reply. I'm still having a little trouble with the use of doubles. It's the first time i've come across them. Is there any good references, or example you suggest I look at, as google seems to be coming up fruitless.
The atan2 tip was perfect. Cannot thank you enough for that one.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|