C ++长双精度打印所有数字 [英] c++ long double printing all digits with precision

查看:77
本文介绍了C ++长双精度打印所有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我的问题,我在这里看到过一篇文章,但由于我不熟悉C ++而无法理解.我写了一个小脚本,该脚本从用户那里获取一个数字,然后该脚本打印出所输入数字的阶乘.一旦输入了较大的数字(例如30),脚本不会输出所有数字.输出类似于2.652528598 E + 32,但是我想要的是确切的数字265252859812191058636308480000000.>

Regarding my question I have seen a post on here but did not understand since i am new to C++. I wrote a small script which gets a number from user and script prints out the factorial of entered number. Once I entered bigger numbers like 30, script does not print out all the digits.Output is like 2.652528598 E+32 however What I want is exact number 265252859812191058636308480000000. Could someone explain how to get all digits in long double.Thanks in advance

推荐答案

您可以将输出流的精度设置为所需的任意值,以便获得所需的结果.

You can set the precision of the output stream to whatever you want in order to get your desired results.

http://www.cplusplus.com/reference/ios/ios_ios/ios/precision/

以下是页面摘录以及代码示例.

Here is an extract from the page, along with a code example.

获取/设置浮点十进制精度浮点精度确定要在插入操作中写入的最大位数,以表示浮点值.这种解释的方式取决于 floatfield 格式标记是设置为特定的符号(固定的还是固定的,还是使用 default 表示法,不一定等于 fixed scientific ).

Get/Set floating-point decimal precision The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).

使用默认的浮点表示法,精度字段指定要计算的总有效位数,该总数将对小数点之前和之后的小数点总数进行计数.请注意,这不是最小值,因此如果显示的数字位数少于精度,则不会在显示的数字后缀零.在固定和科学记数法中,精度字段都精确指定要在小数点后显示多少个数字,即使其中包括尾随的十进制零也是如此.在这种情况下,小数点前的数字与精度无关.

Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision. In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.

还可以使用参数化操纵器 setprecision 修改此十进制精度.

This decimal precision can also be modified using the parameterized manipulator setprecision.

// modify precision
#include <iostream>     // std::cout, std::ios

int main () {
    double f = 3.14159;
    std::cout.unsetf ( std::ios::floatfield );                // floatfield not set
    std::cout.precision(5);
    std::cout << f << '\n';
    std::cout.precision(10);
    std::cout << f << '\n';
    std::cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set to fixed
    std::cout << f << '\n';
    return 0;
}

可能的输出:

3.1416
3.14159
3.1415900000

请注意,第一个数字写的长度只有5位数字,而第二个数字是6位,但是即使流的精度现在为10位也是如此,这是没有更多的.这是因为只有默认 floatfield 的精度指定要显示的最大位数,但不指定最小位数.打印的第三个数字显示小数点后10位,因为 floatfield 格式标记在这种情况下设置为固定.

Notice how the first number written is just 5 digits long, while the second is 6, but not more, even though the stream's precision is now 10. That is because precision with the default floatfield only specifies the maximum number of digits to be displayed, but not the minimum. The third number printed displays 10 digits after the decimal point because the floatfield format flag is in this case set to fixed.

这篇关于C ++长双精度打印所有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆