View unanswered posts | View active topics It is currently Fri Sep 19, 2014 4:47 pm






Reply to topic  [ 4 posts ] 
asin returns null value 
Author Message
Rookie

Joined: Sun Mar 02, 2008 9:41 am
Posts: 25
Post asin returns null value
Hi,

I am trying to use asin to find out the degrees of an angle.
I have the following code but it always return 0.
double degreesToTurn;
degreesToTurn = asin(30);


Would anyone have any ideas why?

I have also try the sin function sothen i could pass the result to asin.

They both return 0.

Regards,
Giannis


Tue Mar 04, 2008 4:59 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
The input value to "asin" needs to be in the range "-1 to +1". Otherwise the result is undefined. You're using 30 as the input!

It sounds like you're really wanting to turn an angle in radians (i.e. value 0 to 2*PI) to degrees (i.e. value 0 to 360). This is not what asin does!! Use the following formula for this conversion.

Code:
int degrees;

degrees = 360 * (radians / (2 * PI));
degrees %= 360;


Tue Mar 04, 2008 5:50 pm
Profile
Rookie

Joined: Sun Mar 02, 2008 9:41 am
Posts: 25
Post Hi
Hi,

I will try this but I have another question since i am completely new to C.

I am trying to do the following:

int i = 20;
int x = 30;

double w = i / x;

And I am not getting the same result.
Couldyou let me know why please?


Regards,
Giannis


Tue Mar 04, 2008 5:59 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post Re: Hi
giannisAPI wrote:
Hi,
int i = 20;
int x = 30;

double w = i / x;

And I am not getting the same result.

C, and consequently ROBOTC, has some funny rules about calculation precisions and number bases. In the above 'i' and 'x' are both 'int' so "i / x' will be calculated using 'int' arithmetic and will result in '0'. The int value '0' will be converted to a 'float' and stored in 'w'.

You probably thought the result should be 0.66667 and were surprised with the zero value.

If you changed to
Code:
w = i / (float) x;

then you would get the fractional result. Because the cast of '(float)' applies to 'x' and then the calculation is performed with 'float' precision.


Tue Mar 04, 2008 7:59 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.