Question

How to convert degrees to radians so that sine would work precisely?

Asked by: blahma

I 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.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-01-12 at 11:43:33ID20453557
Tags

convert

,

degree

,

from

,

radians

Topic

3D Game Programming

Participating Experts
4
Points
50
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PI
    VC5, WinNT4.0 I know that #define PI 3.14159 would be good enough in my programme. Does it make any sense if i do something like this? What is the effect on the compiler/linker ? #define PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998...
  2. sin, cos & tan function
    I found that these function doesn't use degree to get the value. e.g SELECT sin(45) from dual; If 45 is degree, the result should be 0.7071. But Oracle respond 0.525321989 as the result. So, my question in this case, 45 define as what? and how to convert this data typ...
  3. sin(180) not equal to 0
    I have written a trig calculator however everytime the problem of the sin(180) or any of its multiples arise, I always get a number in scientific notation that is quite close to 0, but not 0. How can I fix this, I am converting from degrees to radians correctly, as all my oth...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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.

 

by: MathematixPosted on 2003-01-12 at 18:15:35ID: 7714227

Sorry, I hope you are coding in C/C++ BTW. :)

 

by: jongoldfishPosted on 2003-01-12 at 18:57:09ID: 7714373

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

 

by: MetzkerJL1Posted on 2003-01-13 at 04:52:37ID: 7716799

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.1415926535897932684626433


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

 

by: Madis731Posted on 2003-01-15 at 12:29:57ID: 7735404

I made some calculations. The 30° answer isn't exactly 0.5 but
0,499999999999999999933223179531195
the 180° one is 4,6264338327950288419718286668577e-19
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

 

by: blahmaPosted on 2003-01-17 at 13:12:39ID: 7749905

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

 

by: blahmaPosted on 2003-01-17 at 13:16:34ID: 7749928

Correction: In the last paragraph I meant "noone of you has posted their question as an answer" - rather than "as a question".

 

by: jongoldfishPosted on 2003-01-20 at 08:08:00ID: 7761622

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-exchange.com/Programming/Game_Development/3D_Programming/Q_20453557.html

(information on how to award points to an expert borrowed from Dan Rollins at question: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20443597.html)


Hope this helps,
-Jon..goldfishy

 

by: jongoldfishPosted on 2003-01-20 at 08:10:50ID: 7761648

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

 

by: blahmaPosted on 2003-01-24 at 13:13:12ID: 7803529

It did not solve my problem but rather convinced me it was not a problem at all and assured me that there is no function to convert degrees to radians in VC++.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...