Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
383 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...