C++ Program to Compute High Precision Division for Integers

As we probably are aware, the math activities in programming

dialects are restricted by exactness precision. That implies you won’t get an

precise answer yet an adjusted close answer. For instance, in the event that you have 10 isolated by

3 then you will get answer like 3.33333339555496.

The accompanying program will depict a brisk and effective strategy to

figure the division with high precisions. In reality the space necessity is

just O(1) in any case the quantity of portion places you need. The calculation

intricacy is simply O(n) which is sufficiently quick to recover two or three thousand spots

inside seconds. Be that as it may, the accompanying C++ program will simply print each

part result to screen without putting away it. In the event that you need to additionally control

the outcome, at that point you need an exhibit to store it.

The calculation will reenact the human (as we are educated in

the grade school) doing division calculation. Get the rest of subtract

it from the profit, duplicate by ten and go to the following cycle. Obviously,

on the off chance that we can get the entire outcome, we should not have to proceed until the

pre-characterized number of cycles are reached.

Program

#include <iostream>
using namespace std;
void PrintDiv(int a, int b, int n)
{
                cout
<< a << ” / ” << b << ” = ” ;
                if (b
== 0)
                {
                                //
if divide by 0
                                cout
<< (a >= 0 ? “+INF”: “-INF”) << endl;
                                return;
                }
                if (a
== 0)
                {
                                cout
<< 0 << endl;
                                return;
                }
                if (n
<= 0)
                {
                                //
just the integer part
                                cout
<< a / b << endl;
                                return;
                }
                if (((a
> 0) && (b < 0)) || ((a < 0) && (b > 0)))
                {
                                //
check the sign
                                cout
<< “-“;
                                a
= a > 0 ? a: -a;
                                b
= b > 0 ? b: -b;
                }
                int c =
a / b;
                for
(int i = 0; i < n; i ++) // iterated
                {
                                cout
<< c;
                                a
-= b * c;
                                if
(a == 0) break; // full division no need to continue
                                a
*= 10;
                                c
= a / b;
                                if
(i == 0) cout << “.”;
                }
                cout
<< endl;
}
int main()
{
                cout
<< “Please give me three integers: ” << endl;
                int a,
b, n;
                do {
                                cin
>> a >> b >> n;
                                PrintDiv(a,
b, n);
                } while
(n >= 0);
                cin
>> n;
}

The C++ program requests that you input three whole numbers and it will

take it from that point. The yield of the nearby reasonable number of PI is 355/113

what’s more, the program prints the right answer.

Be noticed that the C++ program additionally checks for some corner

experiments, for example, isolate by zero and it will deal with the sign too.

Underneath outlines some experiments.

It would be ideal if you be noticed that when we check the indications of the information

whole numbers (an and b), we can do it two different ways. First is to check if a*b is

negative. Or then again we can simply register in the event that it falls with two cases

(a<0)&&(b>0) or (a>0)&&(b<0). The second in my

feeling is better on the grounds that the first may really flood and give

off base decisions whenever given numbers are excessively huge.

Leave a Comment

error: Alert: Content is protected!!