3

So, I'm working on a C++ project. I have a var of long double type and assigned it a value like "1.02"

Then, I try to use cout to print it and the result is: -0

I already tried to use setprecision and all I found googling the problem.

What is the solution for this?

Example code:

#include <cstdlib>
#include <iomanip>

using namespace std;

int main(int argc, char** argv)
{

    cout.precision(15);
    long double var = 1.2;
    cout << var << endl;
    return 0;
}

OS: Windows 8.1 64 bits Compiler: minGW IDE: NetBeans 8.0.2

nervousDev
  • 75
  • 3
  • 9
  • 2
    Post the whole program unedited, along with your compiler and OS versions – M.M Dec 18 '14 at 01:30
  • Works for me, what is the version of your compiler? – Nick Louloudakis Dec 18 '14 at 01:47
  • 2
    This is a well-known problem with MinGW. MinGW uses the Microsoft library. Microsoft's compiler implements `long double` as a 64-bit type (identical to `double`) and their library expects the same. Their library simply doesn't understand the 80-bit long-double format that MinGW uses. – Jerry Coffin Dec 18 '14 at 02:15

4 Answers4

2

It seems to be a problem with compiler. Take a look here: http://mingw.5.n7.nabble.com/Strange-behaviour-of-gcc-4-8-1-with-long-double-td32949.html

Use printf or convert a value of your variable to double before passing to cout. (BTW are sure you need 80-bit precision?)

Smylic
  • 149
  • 7
  • 1
    To be more specific, this is a problem with I/O. Eighty-bit longdoubles work just fine. The best way to fix this seems to be with [`printf("%Lg");`](http://stackoverflow.com/a/14871560/1143274) The linked discussion thread suggests that the `-posix` argument to `g++` is required for this to work, but for me it works either way. – Evgeni Sergeev Sep 01 '15 at 05:06
0

This is an easier method, but your program worked on my compiler.

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{

    std::setprecision(10);
    long double var = 1.023563457578;
    cout << var << endl;
    return 0;
}

I hope this helps you see that your compiler might actually have a problem.

Source - > Link

-1

I see nothing wrong at all in the code. I just put it into a standard format and it works. Here is the code assuming what you posted is the entire thing.

#include <iostream>

using namespace std;

int main(){
    long double var = 1.0202;
    cout.precision(5);
    cout << var << endl;
}

I hope this answers your question.

Edit: P.S. Shorter, the better, so I have a better solution (opinionated).

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

int main(){
    long double var = 1.0202;
    //cout.precision(5);
    cout << setprecision(5) << var << endl;
}

I think this one is better since it is shorter. I would also recommend using printf if you are doing more complex decimal stuff since printf can choose which variables (if you have multiple) have decimals or how much.

David G
  • 94,763
  • 41
  • 167
  • 253
bobtheboy
  • 386
  • 12
  • 25
-3

I just figured out the problem.. It was the include of cstdlib instead of iostream.

jscs
  • 63,694
  • 13
  • 151
  • 195
nervousDev
  • 75
  • 3
  • 9