1

The following is my C++ program. I want to store a long number such as pi on a variable so I am trying to use long double. But when I run the program it only displays 3.14159 . How to store the full floating point number to the variable?

#include <iostream>
using namespace std;

int main() {
long double pi;
pi = 3.14159265358979323846264338327950288419716939937510;
cout << "PI = " << pi << endl;
return 0;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You are storing the full floating point value you are just not printing it all out. Try `cout << std::setprecision(30) << pi << '\n';` – Galik Jul 18 '15 at 06:41

3 Answers3

4

Using stream manipulators, it's easy:

#include <iostream>
#include <iomanip>

int main()
{

    long double pi;
    pi = 3.14159265358979323846264338327950288419716939937510L; // L for long double literal

    std::cout << "PI: " << std::setprecision(20) << pi;


}
Russell Greene
  • 2,141
  • 17
  • 29
  • What if I want to multiply two of those numbers with precision 20 –  Jul 18 '15 at 06:49
  • 3
    The internal data is NEVER affected by `std::setprecision`. all it does is set how many digits are printed. You can easily multiple another `long double` by `pi` then print it, so long you use `std::setprecision` – Russell Greene Jul 18 '15 at 06:52
  • when i try to run this if statement' if(i%k!=0&&j%k!=0)' it says invalid operands of types 'long double' and 'long double' to binary 'operator%' –  Jul 18 '15 at 07:01
  • Yes, it is only possible to mod (https://en.wikipedia.org/wiki/Modulo_operation) integer operands. What are you trying to do? – Russell Greene Jul 18 '15 at 07:03
  • I am trying to check for prime multiples which cannot be stored in 4 byte int –  Jul 18 '15 at 07:09
  • try using `uint64_t` defined in `. that is a 64 bit unsigned integer, storing values up to 2^64. Is that big enough? – Russell Greene Jul 18 '15 at 07:11
3

The problem here is even long double has limited precision. Consider this (C++11)

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main() {
    cout.precision(51);
    std::string pi("3.14159265358979323846264338327950288419716939937510");
    cout << pi << endl;
    cout << stold(pi) << endl;
    cout << M_PIl << endl;        /// The constant from <math.h>
}

Output

3.14159265358979323846264338327950288419716939937510
3.14159265358979323851280895940618620443274267017841
                    ^ value changes from here (18th decimal place)
3.14159265358979323851280895940618620443274267017841
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
1

There is no problem(well actually there is a problem with precision) in storing the value in long double. The issue is with printing it.

Try this instead:

cout << "PI = " << setprecision(40) << pi << endl;

If you try the above, you would find that the value actually printed will start losing precision after some decimal places(18-25 I guess).The precision of long double in c/c++ is implementation defined. Thus you need to check your system for the maximum precision long double can store.

Raman
  • 2,735
  • 1
  • 26
  • 46