问题描述
所以,我正在做一个 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;
}
操作系统:Windows 8.1 64 位编译器:minGWIDE: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
或在传递给 cout
之前将变量的值转换为 double
.(顺便说一句,您确定需要 80 位精度吗?)
Use printf
or convert a value of your variable to double
before passing to cout
. (BTW are sure you need 80-bit precision?)
这篇关于Cout 长双发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!