- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有为 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
. 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.
44
当以大端字节顺序查看时,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/
下面的代码有效,我觉得double(double)和double(*)(double)没有区别,square和 &square,我说得对吗? #include double square(doubl
我知道我的作业很草率,这是我在这门课上的第 4 次作业。任何帮助将不胜感激,谢谢。 double getPrincipal(0); double getRate(0); double getYe
我遇到了那个错误,当我使用类时,我在使用函数指针时遇到了这个错误。我的函数'ope'函数我该如何解决 evaluator::function(){ double (*ope) (dou
问题://故事从哪里开始 Graphics 类型中的方法 drawLine(int, int, int, int) 不适用于参数 (double, double, double, double) g.
我有一张 map> m1 形式的 map .我可以将其复制到 map m2 形式的 map 吗?这样键是相同的,并且 m2 中的值是 get(m1->second) 不使用循环?谢谢! 最佳答案 这样
有没有办法获取vector> 的“.first”和“.second”的连续内存? ?我的意思是: void func(int N, double* x, double* y) { for (i
我正在尝试将自定义 lambda 传递给需要函数指针的函数(更准确地说是 zero 中的 Brent library 函数)。 我的想法是,我将使用参数创建一次 lambda,然后用多个值对其求值 x
这是一个很简单的问题,让我很困惑。 我收到一个源文件的以下错误,但另一个没有: 4 src/Source2.cpp:1466: error: no matching function for cal
struct CalculatorBrain { private var accumulator: Double? func changeSign(operand: Double) -
在我正在进行的项目中,我尝试使用 curlpp库来发出一个简单的 html GET 请求。当我将 cpp 文件传递给 g++ 时,出现以下错误: /usr/local/include/curlpp
不使用double就能获得quadruple精度超过16位的数字吗?如果可能的话,这取决于编译器还是其他?因为我知道有人说他使用double精度,并且具有22位精度。 最佳答案 数据类型double
我正在寻找有关特斯拉 GPU 中硬件如何实现 double 的信息。我读到,两个流处理器正在处理单个 double 值,但我没有找到 nvidia 的任何官方论文。 提前致谢。聚苯硫醚为什么大多数 G
这个问题在这里已经有了答案: Passing capturing lambda as function pointer (10 个答案) 关闭 2 年前。 我有这个错误 error: cannot
情况:我有一个元组列表,其中添加了一个元组: List> list = new List>(); list .Add(new Tuple(2.2, 6.6)); 一切似乎都还好。但是......在 D
我有一个 JList,里面有一堆名字,还有一个包含这些名字值的数组 final Double[] filmcost = { 5.00, 5.50, 7.00, 6.00, 5.00 }; 我想做的是,
我试图找出牛顿法来求方程的根。这个错误出来了,我无法处理。 double fn(double n){ return sin(n)+log(n)-1; } double f1n(double n
我有一个 junit 测试断言两个 Double 对象,具有以下内容: Assert.assertEquals(Double expected, Double result); 这很好,然后我决定将其
我正在尝试引入部分数据文件来填充数组,用户尝试了三次输入正确的数据文件名。我一再遇到这些错误。我知道像 arr 这样的数组只是一个指向内存块的指针。 #include #include #incl
我正在尝试完成复习题(为即将到来的编程决赛),但是,我无法解决这个问题,因为我不断收到错误(标题)。正如预期的那样,我将发布问题和我尝试的解决方案。 问题: 给定以下函数定义:void swap(do
任何人都知道如何实现这一目标。我已经尝试了通常的公式,但我只得到正数 Double.NEGATIVE_INFINITY) return d; } } 这将以相同的概率
我是一名优秀的程序员,十分优秀!