0

I need to print out high precision pow() to the console. For example 100-(0.5)^100 is 99.9999999999999999999999999999992111390947789881945882714347172137703267935648909769952297210693359375 according to wolfram alpha.

When I generate like this long double d = (double)std::pow(0.5, 100.0); I get a number in scientific notation. I have also tried using GMP but primality_probability prints out as 100. Any tips on generating and printing high precision pow like this?

mpf_t primality_probability;
mpf_init2 (primality_probability, 256);
mpf_set_d(primality_probability, 0.5);
mpf_pow_ui(primality_probability, primality_probability, repetitions);
mpf_ui_sub(primality_probability, 100, primality_probability);
user2864740
  • 60,010
  • 15
  • 145
  • 220
user2771729
  • 448
  • 1
  • 7
  • 13
  • look into std::setprecision std::fixed c++. https://en.cppreference.com/w/cpp/io/manip/setprecision – Omid CompSCI Feb 28 '20 at 23:12
  • 1
    @OmidCompSci oh wow that worked.. when I tried that before I was foolish and didn't set the precision to enough digits. Thanks! – user2771729 Feb 28 '20 at 23:15
  • Oh nvm it worked once and I got excited. Still having issues: long double d = (long double)std::pow(0.5, 100.0); std::cout << (100 - d) << std::setprecision(256); – user2771729 Feb 28 '20 at 23:19
  • Ensure to show the *current* output such as, hypothetically "7.88861e-31" or "100" (it is different based on both reported results). Use this information to refine the question, and update the title accordinly - ie. is it about high-precision numbers or about the display of such? For example, an [IEEE-754 64-bit double](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) can only store 17 significant decimal digits. – user2864740 Feb 28 '20 at 23:20
  • 1
    `long double` won't have sufficient precision for your needs here. q.v. my answer to a related question: https://stackoverflow.com/questions/50968737 – Eljay Feb 28 '20 at 23:22
  • Also, the cast to `double` before assignment to a `long double` seems .. odd. As does does the overload called. Shouldn't an argument be promoted to a `long double` first (or [use `std::powl` for clarity](https://en.cppreference.com/w/cpp/numeric/math/pow))? – user2864740 Feb 28 '20 at 23:27
  • I suppose yeah I just want to be able to print it in digits more so than the precision after a certain point – user2771729 Feb 28 '20 at 23:28
  • Considering the precision of the operands, how much precision is actually valid? – Thomas Matthews Feb 28 '20 at 23:47
  • 1
    Am I following this right? You are interested in how to format your output, yet your sample code performs no output? – JaMiT Feb 29 '20 at 02:46

0 Answers0