Math.Cos(0.5 * Math.PI) doesn't evaluate exactly to zero

Posted on 2009-04-21
Last Modified: 2012-05-06
In mathematics the cosine of 0.5Pi (in radians) is zero. In C# however the equivalent expression
Math.Cos(0.5 * Math.PI) would evaluate to 6.12303176911189E-17. A value that is very close to zero. I was wondering why it won't evaluate to exactly zero and if there is a way to correct this.

Thanks in advance
Question by:a_anis3000
    LVL 39

    Accepted Solution

    When you work with floating point numbers, then you will notice that everything you are doing is non-exact science. Often you don't notice, because on average, we don't show 15 digits behind the comma. But occasionally, we find ourselves wondering why zero isn't equal to zero...

    To resolve this, there's only so much you can do, and in this case, the two things you can do is to use a Decimal, which has a higher precision then a double, or you can use rounding.

    This is an age old problem that computers have to live with. Only real math packages (like gpari or ubasic, but also mathlabs), who do calculations in a totally different way, have a way out of this.

    -- Abel --
    LVL 39

    Expert Comment

    Another way to look at this: PI has infinitely many decimals, and so does every calculation that involves PI. A computer has only a limited amount of available memory. To keep things workable, the IEEE consortium has invented the IEEE floating points which uses a well-defined behavior for rounding and precision:

    Author Comment

    I guess that is a limitation of floating point numbers that their precision is not perfect.

    I tried using decimal unfortunately the Math.Cos method takes only double parameter so I tried rounding it as suggested and it worked for me.

    Thanks for the tip Abel
    LVL 39

    Expert Comment

    > I guess that is a limitation of floating point numbers that their precision is not perfect.
    see my earlier comment: it is mathematically and technically impossible to have perfect precision. And as long as resources are limited in the world, this will never change.

    glad you found a working method.
    LVL 39

    Expert Comment

    Before grading below A, it is a good idea to give an expert an opportunity to give you all you need to understand a solution. Please read the 10pts must principle of EE and help us, experts, to earn that.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now