Sorry, I hope you are coding in C/C++ BTW. :)
Main Topics
Browse All TopicsI have recently made some attempts for a simple 3D program and have come across a problem with converting degrees to radians and then giving the result to sin() MSVC++ function.
I understand that radians = degrees*pi/180. This works precisely with 30 degrees, e.g., sin(30*pi/180) gives 0.5. But I face a problem when I want the program to calculate the sine of 180 degrees. The result I get should be 0, but is actually a little different. The difference is in some 14th digit after decimal point, however the result is not precisely zero and this makes a mess later.
I think that the problem is caused by a not enough precise value of pi used. I use my own #define pi 3.141592653589793238 as I have not found any pi nor degrees-to-radians function in math.h. I think that probably this is why the input value for the sin() function is not exactly what it would expect it to be in order to return precise zero (i.e. "its pi" radians). But how should I solve this when expanding the decimal part of the pi constant does not help and I don't know any other way how to convert from degrees to radians?
Will welcome any help on this.
Marek in Czech Republic
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
A more generic calculation (but not as computationally fast as for the case where sine=180 as Mathematix points out):
const double epsilon = 1e-15;
double val = sin(radians);
if (fabs(expected-val) < epsilon) {
val = expected;
}
where radians, and expected are either defined earlier, or passed in to method/function.
-Jon
in 3d programming you do need to be precise but at some point you gotta cut your losses...but thats just my opinion. So..my solution would be to use a few more numbers of pi to get more precise.
3.141592653589793268462643
btw, it always did amaze me that c++ didnt come with a built in function to convert degrees to radians considering its sin function take that as a parameter
I made some calculations. The 30° answer isn't exactly 0.5 but
0,499999999999999999933223
the 180° one is 4,626433832795028841971828
it's some 18 zeroes after "0," awfully precise, but still —
for example when player rotates about some axis thousands of times then there are only 14 zeroes and so on...
you'll have to do something like this:
ß>=pi => ß=0
ß<=-pi => ß=0
The precision will remain, but I still don't understand were you need that kind of precision.
NO calculator will find that sin(30)=0.5, because they work with bits!
There is always possibility to use look-up-tables, but it's slower when you have thousands of values stored, but it's your decision.
Hope it helps
Thanks to all you folks who have replied to my question providing various tips on how to solve the problem.
It looks like I am going to use the sine function even if it does not return precise numbers. It's true that I don't need such a precision as you have pointed out - the sine function is used to calculate the camera position after a move in the given direction. I will probably choose a way of rounding the number to a finite number of decimal digits so the imprecisions will get smoothed.
I just posted this question here because I was confused by the fact that I am not able to calculate the sine of 180 degrees as a precise zero as it should be. I thought I had to be making something badly or using an incorrect method for converting degrees to radians... I thought that there just had to be a certain value of pi that the sin() function itself used and that if I had fond this value, the function would return precise 0 as it was supposed to do. Perhaps I am not able to fully appreciate the power of radians now, but I still think that if the VC++ mathematic functions used degrees rather than radians, the work would be much more convenient as it would be sure that sin(180)==0 etc. and you would not have to spend time doing the conversions back and forth.
If you have any other ideas guys, I am willing to listen, as I have almost no expertise at this field (being a programmer in general for already eight years, however) and I am a high school sophomore student, so do not know as much about maths as you here do.
And yet another question: I am a newbie to this site (Experts-Exchange). Could you please tell me whether I should give the 50 points I have assigned to this question to anyone of you (who?) or should I keep it as noone of you has posted their question as a question, only as a comment?
Thanks in advance.
Marek
blahma, et. al.,
I am also somewhat new to this site, but I do have an opinion on this topic.
As for awarding points, my suggestion is to do one of these things:
1. Look again carefuly at the responses to your question. Ask yourself if one particular comment helped you to either understand the question you were asking, or pointed you in the right direction. If this is the case, then award the points to that person;
2. If the above isn't the case, I favour awarding points to people who tried, and supplied help and information. I suggest awarding some points to each person you felt contributed in an informative way. According to DanRollins, you can award points to any expert by this mechanism:
"To give points to an Expert, ask a new question in the same Topic Area. Title it "Points for XxxXxxxxxx" and (this is important) in the body of the question, provide a link to *this* question."
Jongoldifsh says: Here is a link to this question, so you can cut-and-paste:
http://www.experts-exchang
(information on how to award points to an expert borrowed from Dan Rollins at question: http://www.experts-exchang
Hope this helps,
-Jon..goldfishy
Oh yeah, one other thing-- I think that if you do elect option 2 as I describe above, there is some way "official" way to cancel/cleanup your question. But then I suggest you go to the "Community Support >> CleanUp" area and ask them to do it, or to explain how, or read through the various questions&faqs on this site.
-Jon..goldfishy
Business Accounts
Answer for Membership
by: MathematixPosted on 2003-01-12 at 18:14:39ID: 7714223
The only solution that comes to mind, that will guarantee the result that you would wish for is:
if(sine = 180.0f)
radians = 0.0f;
Due to your finite expansion of pi any result returned will not be zero.