长双增量运算符不处理大数 < >我将C++系统从Solaris(Sun Box和Solaris编译器)转换为Linux(英特尔BOX和GCC编译器)。在处理大的“长双精度”值时,我遇到了几个问题。(由于一些非常大的整数,我们使用“长双精度”……而不是任何十进制精度)。它以几种奇怪的方式表现出来,但我已将其简化为以下程序。它试图增加一个数字,但没有。我没有得到任何编译或运行时错误。。。它只是不增加数字
我还随机尝试了一些不同的编译器开关(-malign-double和-m128 bit-long-double,打开和关闭它们的各种组合),但没有区别 我也在gdb中运行了这个命令,gdb的“print”命令显示了与cout语句相同的值 有人见过这种行为吗 谢谢 编译命令 SimpleLongDoubleTest.C长双增量运算符不处理大数 < >我将C++系统从Solaris(Sun Box和Solaris编译器)转换为Linux(英特尔BOX和GCC编译器)。在处理大的“长双精度”值时,我遇到了几个问题。(由于一些非常大的整数,我们使用“长双精度”……而不是任何十进制精度)。它以几种奇怪的方式表现出来,但我已将其简化为以下程序。它试图增加一个数字,但没有。我没有得到任何编译或运行时错误。。。它只是不增加数字,c++,linux,gcc,double,long-integer,C++,Linux,Gcc,Double,Long Integer,我还随机尝试了一些不同的编译器开关(-malign-double和-m128 bit-long-double,打开和关闭它们的各种组合),但没有区别 我也在gdb中运行了这个命令,gdb的“print”命令显示了与cout语句相同的值 有人见过这种行为吗 谢谢 编译命令 SimpleLongDoubleTest.C #包括 #包括 #包括 #包括 #包括 int main(int argc,char*argv[]) { std::cout这是预期行为。浮点、双精度、长双精度等在内部以(2^exp
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
std::cout这是预期行为。浮点、双精度、长双精度等在内部以(2^exp偏差)的形式表示*1+xxxxx,其中xxxxx是一个N位二进制数,其中N=23表示浮点数,52表示双精度,可能64表示长双精度。当数字增长大于2^N时,无法再向该变量添加“1”——只能添加2^(N-N)的倍数
您的体系结构也可能将longdouble等同于double(即使x86可以在内部使用80位double)
另请参见--128位double是一个异常,而不是一个规范.如果你想做整数运算,你需要使用整数类型。看起来像是Aki搞定了。由于128位的支持,这在Sun/Solaris/Sparc上一直有效。我需要重构…我可能会使用libquadmath或GMP…或者退一步,看看我是否可以更改要求,这样我就不会处理如此大的数字了首先,感谢大家的快速响应……可能的困惑在于假设x86体系结构中的“128位双精度”的尾数比64位大得多——但由于80位双精度之外没有本机支持,而且由于x64系统确实将堆栈与16字节边界对齐,128位双精度很可能double实际上意味着80位双精度,16字节对齐。
$ /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
#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 ;
}
}