C ++ long double print all digits with precision

As for my question, I saw the post here, but did not understand, since I am new to C ++. I wrote a small script that gets the number from the user, and the script displays the factorial of the entered number. As soon as I entered a larger number, for example 30, the script does not print all the digits. It turns out as 2.652528598 E + 32, but I want it to be the exact number 265252859812191058636308480000000. Can someone explain how to get all the digits in a long double. thanks in advance

+4
source share
1 answer

You can set the accuracy of the output stream to whatever you want to get the desired results.

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

, .

/ , , . , , , floatfield ( fixed scientific), ( default, fixed scientific).

, , , . , , , , . , , , . .

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 .

+5

Source: https://habr.com/ru/post/1599690/


All Articles