整数高精度除法_C ++程序,用于计算整数的高精度除法

整数高精度除法

arithmetic operations in programming 算术运算
languages are limited by precision accuracy. That means you will not get an
语言受精度限制。 这意味着您不会得到
exact answer but a rounded-close answer. For example, if you have 10 divided by
确切的答案,但四舍五入的答案。 例如,如果您有10除以
3 then you will get answer like 3.33333339555496.
3,那么您将得到像3.33333339555496这样的答案。

compute the division with high precisions. Actually the space requirement is
计算精度高。 实际上空间需求是
just O(1) regardless the number of fraction places you want. The algorithm
只需O(1)即可,无论您想要的分数位数是多少。 算法
complexity is just O(n) which is fast enough to retrieve a few thousand places
复杂度仅为O(n),足以检索数千个位置
within seconds. However, the following
在几秒钟内。 但是,以下 C++ program will just print each C ++程序将仅打印每个
fraction result to screen without storing it. If you want to further manipulate
分数结果不存储到屏幕。 如果要进一步操作
the result, then you need an array to store it.
结果,那么您需要一个数组来存储它。

the primary school) doing division computation. Get the remainder and subtract
小学)进行除法运算。 得到余数并减去
it from the dividend, multiply by ten and go to the next iteration. Of course,
从红利中乘以10,然后进行下一个迭代。 当然,
if we can get the whole result, we may not need to continue until the
如果我们可以获得全部结果,则可能无需继续操作,直到
pre-defined number of iterations are reached.
达到预定义的迭代次数。
#include <iostream> #include <iostream>
using namespace std; 使用命名空间std;
void PrintDiv(int a, int b, int n) void PrintDiv(int a,int b,int n)
{ {
                cout
<< a << ” / ” << b << ” = ” ;
out << a <<” /“ << b <<” =”;
                if (b
== 0)
如果(b == 0)
                { {
                                //
if divide by 0
// 如果除以0
                                cout
<< (a >= 0 ? “+INF”: “-INF”) << endl;
out <<(a> = 0?“ + INF”:“ -INF”)<< endl;
                                return; 返回;
                } }
                if (a
== 0)
如果一个 == 0)
                { {
                                cout
<< 0 << endl;
out << 0 << endl;
                                return; 返回;
                } }
                if (n
<= 0)
如果(n <= 0)
                { {
                                //
just the integer part
// 只是整数部分
                                cout
<< a / b << endl;
out << a / b << endl;
                                return; 返回;
                } }
                if (((a
> 0) && (b < 0)) || ((a < 0) && (b > 0)))
如果一个 > 0)&&(b <0))|| ((a <0)&&(b> 0)))
                { {
                                //
check the sign
// 检查标志
                                cout
<< “-“;
out <<“-”;
                                a
= a > 0 ? a: -a;
一个 = a> 0? a:-a;
                                b
= b > 0 ? b: -b;
b = b> 0? b:-b;
                } }
                int c =
a / b;
整数c = a / b;
                for
(int i = 0; i < n; i ++) // iterated
对于 (int i = 0; i <n; i ++)//迭代
                { {
                                cout
<< c;
out << c;
                                a
-= b * c;
一个 -= b * c;
                                if
(a == 0) break; // full division no need to continue
如果 (a == 0)中断; //完全除法,无需继续
                                a
*= 10;
一个 * = 10;
                                c
= a / b;
C = a / b;
                                if
(i == 0) cout << “.”;
如果 (i == 0)cout <<“。”;
                } }
                cout
<< endl;
out << endl;
} }
int main() int main()
{ {
                cout
<< “Please give me three integers: ” << endl;
out <<“请给我三个整数:” << endl;
                int a,
b, n;
诠释 b,n;
                do { 做{
                                cin
>> a >> b >> n;
cin >> a >> b >> n;
                                PrintDiv(a,
b, n);
PrintDiv(a, b,n);
                } while
(n >= 0);
}而 (n> = 0);
                cin
>> n;
cin >> n;
} }

take it from there. The output of the close rational number of PI is 355/113
从那里拿走。 PI的接近有理数的输出为355/113
and the program prints the correct answer.
程序将打印正确的答案。
C++ Program to Compute High Precision Division for Integers

test cases such as divide by zero and it will take care of the sign as well.
测试用例(例如被零除),它也会处理符号。
Below illustrates some test cases.
下面说明了一些测试用例。
C++ Program to Compute High Precision Division for Integers

integers (a and b), we can do it two ways. First is to check if a*b is
整数(a和b),我们可以通过两种方式实现。 首先是检查a * b是否为
negative. Or we can just check if it falls into two cases
负。 或者我们可以检查一下是否分为两种情况
(a<0)&&(b>0) or (a>0)&&(b<0). The second in my
(a <0)&&(b> 0)或(a> 0)&&(b <0)。 我的第二个
opinion is better because the first one may actually overflow and give
意见更好,因为第一个可能实际上溢出并给出
incorrect judgements if
如果 given numbers are too large. 给定数字太大,将导致错误的判断。
Author’s bio: 作者简介:
Zhihua Lai is now a Marie Curie Experienced Researcher at
University of Sheffield. At his spare time, he writes blogs and programs just
for fun. He is also an ACMer where he is keen on fast and efficient algorithms. 
His blog can be found at http://codingforspeed.com/
赖志华现在是Marie Curie的资深研究员 谢菲尔德大学。 业余时间,他只是写博客和程序 为了娱乐。 他还是ACMer,他热衷于快速高效的算法。 他的博客可以在http://codingforspeed.com/找到。

翻译自: https://www.thecrazyprogrammer.com/2013/10/cpp-high-precision-division-program.html

整数高精度除法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值