C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 5th, 2013, 05:19 PM
Butterbiscuit Butterbiscuit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 Butterbiscuit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 5th, 2013, 06:43 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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.

Reply With Quote
  #3  
Old January 6th, 2013, 06:40 AM
Butterbiscuit Butterbiscuit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 Butterbiscuit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help with number conversion

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap