我正在将 C++ 系统从 solaris(SUN box 和 solaris 编译器)转换为 linux(intel box 和 gcc 编译器)。在处理大的“long double”值时,我遇到了几个问题。(由于一些非常非常大的整数,我们使用“long double”......不是任何小数精度)。它以几种奇怪的方式表现出来,但我已将其简化为以下程序。它试图增加一个数字,但没有。我没有得到任何编译或运行时错误......它只是没有增加数字。
我还随机尝试了一些不同的编译器开关(-malign-double 和 -m128bit-long-double 以及这些开关的各种组合打开和关闭),但没有区别。
我也在 gdb 中运行过它,gdb 的“打印”命令显示与 cout 语句相同的值。
有人见过这种行为吗?
谢谢
编译命令
$ /usr/bin/c++ --version
c++ (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)
$ /usr/bin/c++ -g -Wall -fPIC -c SimpleLongDoubleTest.C -o SimpleLongDoubleTest.o
$ /usr/bin/c++ -g SimpleLongDoubleTest.o -o SimpleLongDoubleTest
$ ./SimpleLongDoubleTest
Maximum value for long double: 1.18973e+4932
digits 10 = 18
ld = 1268035319515045691392
ld = 1268035319515045691392
ld = 1268035319515045691392
ld = 1268035319515045691392
ld = 1268035319515045691392
SimpleLongDoubleTest.C
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <limits>
#include <iomanip>
int main( int argc, char* argv[] )
{
std::cout << "Maximum value for long double: "
<< std::numeric_limits<long double>::max() << '\n';
std::cout << "digits 10 = " << std::numeric_limits<long double>::digits10
<< std::endl;
// this doesn't work (there might be smaller numbers that also doen't work...
// That is, I'm not sure the exact number between this and the number defined
// below where things break)
long double ld = 1268035319515045691392.0L ;
// but this or any smaller number works (there might be larger numbers that
// work... That is, I'm not sure the exact number between this and the number
// defined above where things break)
//long double ld = 268035319515045691392.0L ;
for ( int i = 0 ; i < 5 ; i++ )
{
ld++ ;
std::cout << std::setiosflags( std::ios::fixed )
<< std::setprecision( 0 )
<< "ld = " << ld
<< std::endl ;
}
}