我有为 ARMv5TE 构建的 Qt 4.4.3。我尝试将 double
转换为 QString
:
#include <QtCore/QtCore>
#include <cmath>
int main(int argc, char** argv)
{
const double pi = M_PI;
qDebug() << "Pi is : " << pi << "\n but pi is : " << QString::number(pi, 'f', 6);
printf("printf: %f\n",pi);
return 0;
}
但得到奇怪的输出:
Pi is : 8.6192e+97
but pi is : "86191995128153827662389718947289094511677209256133209964237318700300913082475855805240843511529472.0000000000000000"
printf: 3.141593
如何获得正确的字符串?
最佳答案
这看起来是一种字节顺序问题,但不是普通的大端与小端问题。 ARM 有时会对 double
使用不寻常的字节顺序。来自 Jean-Michel Muller 等人的“浮点运算手册”:
... the double-precision number that is closest to
-7.0868766365730135 x 10^-268
is encoded by the sequence of bytes11 22 33 44 55 66 77 88
in memory (from the lowest to the highest one) on x86 and Linux/IA-64 platforms (they are said to be little-endian) and by88 77 66 55 44 33 22 11
on most PowerPC platforms (they are said to be big-endian). Some architectures, such as IA-64, ARM, and PowerPC are said to be bi-endian. i.e., they may be either little-endian or big-endian depending on their configuration.There exists an exception: some ARM-based platforms. ARM processors have traditionally used the floating-point accelerator (FPA) architecture, where the double-precision numbers are decomposed into two 32-bit words in the big-endian order and stored according to the endianess of the machine, i.e., little-endian in general, which means that the above number is encoded by the sequence
55 66 77 88 11 22 33 44
. ARM has recently introduced a new architecture for floating-point arithmetic: vector floating-point (VFP), where the words are stored in the processor's native byte order.
当以大端字节顺序查看时,M_PI
的表示形式如下:
0x400921fb54442d18
由 8.6192e+97
近似的大数将具有如下所示的表示形式:
0x54442d18400921fb
如果仔细观察,两个 32 位字交换了位置,但 32 位字内的字节顺序是相同的。很明显,ARM“传统”双点格式似乎混淆了 Qt 库(或者 Qt 库配置错误)。
我不确定处理器是否使用传统格式并且 Qt 期望它采用 VFP 格式,或者情况是否相反。但它似乎是这两种情况之一。
我也不确定如何解决这个问题 - 我猜想有一些选项可以构建 Qt 来正确处理这个问题。
以下代码段至少会告诉您编译器使用的 double
格式,这可能会帮助您缩小 Qt 中需要更改的范围:
unsigned char* b;
unsigned char* e;
double x = -7.0868766365730135e-268;
b = (unsigned char*) &x;
e = b + sizeof(x);
for (; b != e; ++b) {
printf( "%02x ", *b);
}
puts("");
一个普通的小端机器将显示:
11 22 33 44 55 66 77 88
更新更多分析:
目前,我无法对此执行任何真正的调试(目前我什至无法访问我的工作站),但通过查看 http://qt.gitorious.org 上可用的 Qt 源代码这是额外的分析:
看起来 Qt 调用了 qlocale.cpp 中的 QLocalePrivate::doubleToString()
函数,将 double
转换为字母数字形式。
如果 Qt 编译时定义了 QT_QLOCALE_USES_FCVT
,那么 QLocalePrivate::doubleToString()
将使用平台的 fcvt()
函数来执行转换。如果 QT_QLOCALE_USES_FCVT
未定义,则 QLocalePrivate::doubleToString()
最终调用 _qdtoa()
来执行转换。该函数直接检查 double
的各个字段,似乎假定 double
是严格的大端或小端形式(例如,使用 getWord0()
和 getWord1()
函数分别获取 double
的低位和高位字。
参见 http://qt.gitorious.org/qt/qt/blobs/HEAD/src/corelib/tools/qlocale.cpp和 http://qt.gitorious.org/qt/qt/blobs/HEAD/src/corelib/tools/qlocale_tools.cpp或您自己的文件拷贝以获取详细信息。
假设您的平台正在为 double
使用传统的 ARM FPA 表示(其中 double
的 32 位半部分以大端顺序存储,无论是否整个系统是小端),我认为您需要使用定义的 QT_QLOCALE_USES_FCVT
构建 Qt。我相信您需要做的就是在构建 Qt 时将 -DQT_QLOCALE_USES_FCVT
选项传递给配置脚本。
关于c++ - 在 ARM 上的 Qt 中从 double 到 QString 的错误转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10142684/