Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Win32
Tip/Trick

Reciprocal, Division and Show More Digits than Standard Precision

Rate me:
Please Sign up or sign in to vote.
3.91/5 (3 votes)
8 Sep 2013CPOL1 min read 13.5K   5   2
How to show digits of reciprocal of a number (or division) in more precision

Introduction

Normally, when we work with real numbers, we can show only limited number of digits after the decimal point and this depends on the precision. Sometimes we need to show more digits, especially while dividing two numbers. So, using this method, we can show more digits than standard precision.

Method and Code

The method is very easy and simple. If we want to get the digits of p/q.. Firstly, we find the integral part of division p/q and the remain. Then, for each step, extend the remain by 10, and find the next digit by dividing it by number (denominator). Repeat this calculation by numDigits times.

This method is also used to get digits of reciprocal of a number.

Example: Let's get the digits of 1/7.

Step 0: p=1, q=7, m=[p/q]=[1/7]=0, r=p-m*q=1-0*7=1 [...] integer part

Step 1: p=1*10=10, q=7, m=[p/q]=[10/7]=1, r=p-m*q=10-1*7=3

Step 2: p=3*10=30, q=7, m=[p/q]=[30/7]=4, r=p-m*q=30-4*7=2

Step 3: p=2*10=20, q=7, m=[p/q]=[20/7]=2, r=p-m*q=20-2*7=6

Step 4: p=6*10=60, q=7, m=[p/q]=[60/7]=8, r=p-m*q=60-8*7=4

Step 5: p=4*10=40, q=7, m=[p/q]=[40/7]=5, r=p-m*q=40-5*7=5

Step 6: p=5*10=50, q=7, m=[p/q]=[50/7]=7, r=p-m*q=50-7*7=1

and so on..

From here, we get 1/7 = 0.142857...

Note: You must put some extra checks for negative numbers. Assuming that p and q are positive numbers.

Here is the code:

C#
using System;
using System.Globalization;

class Reciprocal
{

    public static void Main()
    {
        int numDigits = 2000; // number of digits after decimal point

        decimal p = 54321m, q = 12345m; // decimal type, not double or float

        Console.WriteLine("p/q = {0}/{1} ({2} digits after decimal point)", p, q, numDigits);

        // integer part (i.e. before decimal point)
        decimal m = (int)(p / q);
        decimal r = p - m * q;
        p = r * 10;
        Console.Write("{0}.", m);

        // show digits of reciprocal q (ie 1/q)
        for (int i = 0; i < numDigits; i++)
        {
            m = (int)(p / q);
            r = p - m * q;
            p = r * 10;
            Console.Write("{0}", m);
        }

        Console.ReadLine();
    }
} 

Result is:

p/q = 54321 / 12345 (2000 digits after decimal point)
4.400243013365735115431348724179829890643985419198055893074119076549210206561360
87484811664641555285540704738760631834750911300121506682867557715674362089914945
32199270959902794653705953827460510328068043742405832320777642770352369380315917
37545565006075334143377885783718104495747266099635479951397326852976913730255164
03402187120291616038882138517618469015795868772782503037667071688942891859052247
87363304981773997569866342648845686512758201701093560145808019441069258809234507
89793438639125151883353584447144592952612393681652490886998784933171324422843256
37910085054678007290400972053462940461725394896719319562575941676792223572296476
30619684082624544349939246658566221142162818955042527339003645200486026731470230
86269744835965978128797083839611178614823815309842041312272174969623329283110571
08140947752126366950182260024301336573511543134872417982989064398541919805589307
41190765492102065613608748481166464155528554070473876063183475091130012150668286
75577156743620899149453219927095990279465370595382746051032806804374240583232077
76427703523693803159173754556500607533414337788578371810449574726609963547995139
73268529769137302551640340218712029161603888213851761846901579586877278250303766
70716889428918590522478736330498177399756986634264884568651275820170109356014580
80194410692588092345078979343863912515188335358444714459295261239368165249088699
87849331713244228432563791008505467800729040097205346294046172539489671931956257
59416767922235722964763061968408262454434993924665856622114216281895504252733900
36452004860267314702308626974483596597812879708383961117861482381530984204131227
21749696233292831105710814094775212636695018226002430133657351154313487241798298
90643985419198055893074119076549210206561360874848116646415552855407047387606318
34750911300121506682867557715674362089914945321992709599027946537059538274605103
28068043742405832320777642770352369380315917375455650060753341433778857837181044
95747266099635479951397326852976913730255164034021871202916160388821385176184690
15

Visual Studio 2010, C# Console Application

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
YvesDaoust16-Sep-13 3:13
YvesDaoust16-Sep-13 3:13 
QuestionImprovement suggestion Pin
YvesDaoust16-Sep-13 3:12
YvesDaoust16-Sep-13 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.