1

I defined a variable as long double to use the maximum precision of number PI, but when the program gets excecuted, the value of the variable changed to 3.141592653589793116 and lost some precision.

I checked the size of variable , it is same as size of double so that works like double.

I have problem to store this value, not for print that. Now , how can I use long double with full precision?

#include <iostream>
#include <iomanip>

using namespace std;

  int main() {
    //number PI
    long double pi = 3.14159265358979323846264338327950;

    int sz1 = sizeof(long double);
    int sz2 = sizeof(pi);

    cout << sz1 << endl;
    cout << sz2 << endl;
    printf_s("%.20f\n", pi);
    cout << setprecision(20) << pi; 
    return 0;
}

//results:
8
8
3.141592653589793116
3.141592653589793116

Thomas Baruchel
  • 7,236
  • 2
  • 27
  • 46
  • 5
    `long double` is only going to get you something like 18 decimal digits of precision. [See here for more](http://stackoverflow.com/q/476212/2065121). You also need a "L" suffix on your literal. – Roger Rowland Nov 29 '15 at 12:40
  • 2
    http://stackoverflow.com/questions/1380653/why-do-you-need-to-append-an-l-or-f-after-a-value-assigned-to-a-c-constant looks like a more relevant previous answer. – JSF Nov 29 '15 at 12:41
  • 2
    The precision of the floating point types is platform-dependent and not prescribed by the Standard. – Kerrek SB Nov 29 '15 at 12:45
  • 1
    On some platforms `long double` uses x86 80-bit floating point. You apparently aren't using such a platform. Read your compiler documentation to see if some optional support is available. But after getting that right, you still need to deal with the issue whose answer I linked in my first comment. – JSF Nov 29 '15 at 12:46
  • 2
    Relevant to this question would be "what target system" and "what compiler"? – Mats Petersson Nov 29 '15 at 12:52
  • I did a quick google search and the best suggestions for use of 80-bit doubles in Windows appear to be "download and use cygwin" instead of the Microsoft compiler. Is 80-bit enough for you? And is fast 80-bit worth the trouble of installing cygwin? Easier and more precise (but slower) results can be had by using some extended precision floating point package. – JSF Nov 29 '15 at 12:54
  • target is x86 , and compiler is visual studio 2015 –  Nov 29 '15 at 13:02
  • yes ,80-bit is enough. –  Nov 29 '15 at 13:06

0 Answers0