简体   繁体   English

如何在c ++中将精度设置为100位?

[英]How do I set precision to 100 digits in c++?

I am sorry, I have a very basic question. 对不起,我有一个非常基本的问题。

Here is a very simple calculation, an integer (22) divided by another integer (7). 这是一个非常简单的计算,一个整数(22)除以另一个整数(7)。 I would like the c++ code use exactly 100 digits of precision both in the actual calculation inside the code and in the output value. 我希望c ++代码在代码内部和输出值中的实际计算中都使用精确的100位精度。 Using the Maple program, I get 使用Maple程序,我得到了

restart;
Digits:=100;
evalf(22/7);

And the answer I get is, 我得到的答案是,

3.142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857143

But I use the following c++ code and get something different answer. 但我使用以下c ++代码并得到一些不同的答案。 This simply adds zeros after few actual digits, which is completely wrong. 这只是在几个实际数字之后添加零,这是完全错误的。 Below is my c++ code. 下面是我的c ++代码。

#include <iostream>
 using namespace std;
 int main()
 {
     cout << fixed;
     cout.precision(100);
     cout << "22/7 = " << 22/(double)(7) << endl;
     return 0;
}

Using the Maple program, I get 使用Maple程序,我得到了

Maple is a special tool that beside others: Maple是一种特殊的工具,除了其他之外:

Support for symbolic and numeric computation with arbitrary precision 支持任意精度的符号和数值计算

On another side C++ is a general purpose programming language. 另一方面,C ++是一种通用编程语言。 As a language it supports floating point arithmetic which has limited precision. 作为一种语言,它支持精度有限的浮点运算。 You cannot expect to throw arbitrary precision there and expect the same result as you get in Maple. 你不能期望在那里抛出任意精度,并期望得到与Maple相同的结果。 So you need to use an arbitrary precision library (which is not part of standard C++) or switch to a different language that supports it by design. 因此,您需要使用任意精度库(不是标准C ++的一部分)或切换到支持它的其他语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM