考特长双刊 [英] Cout long double issue
问题描述
所以,我正在从事C ++项目.我有一个long double类型的var,并为其分配了一个值,例如"1.02"
So, I'm working on a C++ project. I have a var of long double type and assigned it a value like "1.02"
然后,我尝试使用cout打印它,结果是:-0
Then, I try to use cout to print it and the result is: -0
我已经尝试使用setprecision,并且发现了所有问题.
I already tried to use setprecision and all I found googling the problem.
对此有什么解决方案?
示例代码:
#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位 编译器:minGW IDE:NetBeans 8.0.2
OS: Windows 8.1 64 bits Compiler: minGW IDE: NetBeans 8.0.2
推荐答案
似乎是编译器有问题.在这里看看: http://mingw.5.n7.nabble.com/Strange-behaviour-of-gcc-4-8-1-with-long-double-td32949.html
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
使用printf
或将变量的值转换为double
,然后再传递给cout
. (顺便说一句,您确定需要80位精度吗?)
Use printf
or convert a value of your variable to double
before passing to cout
. (BTW are sure you need 80-bit precision?)
这篇关于考特长双刊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!