Welcome to Dream.In.Code
Become a C# Expert!

Join 137,429 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,892 people online right now. Registration is fast and FREE... Join Now!




HugeDecimals?: Taylor Series

 
Reply to this topicStart new topic

HugeDecimals?: Taylor Series

qdoom
6 Jan, 2008 - 11:15 AM
Post #1

D.I.C Head
**

Joined: 31 Aug, 2006
Posts: 82


My Contributions
I'm writing a program, that calculates cos(1), using Taylor series. That would really be not that hard:

CODE
using System;

namespace TaylorSeriesCosOne
{
    class Program
    {
        static void Main(string[] args)
        {
            double cosOne = 1;
            int n = 2;
            int factorial = 1;
            double power = 1;

            while (n <= 14)
            {
                power = power * -1;
                factorial = factorial * n * (n - 1);
                cosOne = cosOne + (power / factorial);

                Console.WriteLine("n: {0} \tcosOne: {1}", n, cosOne);

                n = n + 2;
            }

            Console.WriteLine("\ncos(1) = {0}", cosOne);

            Console.ReadLine();
        }
    }
}


But... The problem: it's not really an accurate answer. And I want it to be a lot more accurate (more than 10^(-1000), for example). My first thought was to write HugeInteger and HugeDecimal classes (well, parts of them, that I would use in this program). I'm just not sure, if that's the easiest way to do it. I'm not in a hurry with this, so I thought, that I'll just post here and ask if anyone have any suggestions.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: HugeDecimals?: Taylor Series
6 Jan, 2008 - 12:08 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
There's nothing native that I'm aware of... However, really, really big numbers are required for cryptography, so that could be a starting point.

A quick google found me few BigInt type classes:
http://blogs.msdn.com/bclteam/archive/2007...nbar-gazit.aspx
http://www.codeproject.com/KB/cs/biginteger.aspx
http://www.developerfusion.co.uk/show/4670/

Hope this helps.

User is offlineProfile CardPM
+Quote Post

qdoom
RE: HugeDecimals?: Taylor Series
8 Jan, 2008 - 05:43 AM
Post #3

D.I.C Head
**

Joined: 31 Aug, 2006
Posts: 82


My Contributions
Thanks.

I'm done with BigInt (well, the +/- etc. part of it). Now, I have the fraction (cosOne.power / cosOne.factorial) in BigInt. The only thing, that's left is division (cosOne.power / cosOne.factorial to get the answer become decimal from fraction). Time to get a sheet of paper and figure out how to do that one. If anyone has any suggestions (or pseudocode tongue.gif), please don't hesitate to post a reply. rolleyes.gif

This post has been edited by qdoom: 8 Jan, 2008 - 11:49 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:52AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month