6

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

Watzinki
  • 109
  • 1
  • 8
  • 265252859812191058636308480000000 is greater than 2¹⁰⁷. Note that even when `long double` is implemented in the extended-precision x86 format, it can only exactly represent integers up to 2⁶³. – Don Reba Jul 26 '15 at 01:55
  • Thanks Don. My result's first 18/19 digits are correct. if result is more than 19 digits, rest of the numbers are wrong.This is due to 2^63 has only 19 numbers?Do you know how to fix this problem?Thanks in advance – Watzinki Jul 26 '15 at 02:07
  • `long double` has different precision depending on platform, but mostly 64 bits on x86. You can only store exactly all integers up to 2^64 (not 2^63), moreover it can only be precise to ~19 digits. It doesn't have infinite precision, so you can't print such large numbers. If you want to go beyond that, you'll need to use a [tag:bignum] or [tag:biginteger] library – phuclv Jul 26 '15 at 02:13
  • Note that 80-bit registers from x87 are [deprecated for at least ten years](http://stackoverflow.com/questions/3206101/extended-80-bit-double-floating-point-in-x87-not-sse2-we-dont-miss-it). That's why *long double* can become 64-bit in some cases. – stgatilov Jul 26 '15 at 06:24
  • It is the 80-bit registers that are precise for *integers* up to 2⁶⁴. (Sorry, I was off by one in the previous comment.) Regular `double` is good for up to 2⁵³, and `float` up to 2²⁴. There are various bignum libraries out there, including `boost::multiprecision`. – Don Reba Jul 26 '15 at 07:27

1 Answers1

7

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_base/precision/

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

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.

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;
}

Possible output:

3.1416
3.14159
3.1415900000

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.

EmeryBerger
  • 3,897
  • 18
  • 29
  • Thank you very much for your reply.It worked. However, I have another problem which is numbers are not correct after first 18 digits. For example, in case 33! true result is =8683317618811886495518194401280000000, but my result is 8683317618811886496153221372728836096. – Watzinki Jul 26 '15 at 02:02
  • this doesn' help, as the OP's requirement exceeds typical `long double`'s range. You're just setting up the number printed digits – phuclv Jul 26 '15 at 02:15