gpt4 book ai didi

c++ - 四元数乘法错误

转载 作者:行者123 更新时间:2023-12-01 14:53:21 25 4
gpt4 key购买 nike

我实现了四元数,并对其进行了测试以使用Boost。我对它进行循环测试,并且在乘法和除法中总是有错误。然后我用Wolfram找出谁错了,我发现提升是错误的。

我不确定boost库是否是Wong,也许有人可以了解发生了什么?

控制台输出示例:

0
Operation : [ * ]
First vector = [ -41.4168 -92.2373 -33.0126 -42.2364 ]
Second vector = [ -67.8087 -60.3523 58.8705 36.3265 ]
My multiplication = [ 719.45 10041.3 5700.03 -6062.97 ]
Boost multiplication = [ 719.45 10041.3 10041.3 5700.03 ]

main.cpp
#include "quaternion.h"
#include <boost/math/quaternion.hpp>
#include <random>
#include <time.h>
#include <QDebug>

using boost::math::quaternion;

double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}

bool isEqual(double a, double b){
return std::abs(a-b) < std::numeric_limits<double>::epsilon();
}

bool isEqual(Quaternion& quat1, quaternion<double> quat2){
if (!isEqual(quat2.R_component_1(), quat1.real)) return false;
if (!isEqual(quat2.R_component_2(), quat1.imagine.data.at(0))) return false;
if (!isEqual(quat2.R_component_3(), quat1.imagine.data.at(1))) return false;
if (!isEqual(quat2.R_component_4(), quat1.imagine.data.at(2))) return false;
return true;
}

int main () {

std::srand(std::time(nullptr));
Quaternion compR;
quaternion<double> boost;

double a1, a2, a3, a4,
b1, b2, b3, b4;
try {
for (unsigned int i=0; i<10000000; ++i){
a1 = fRand(-100, 100);
a2 = fRand(-100, 100);
a3 = fRand(-100, 100);
a4 = fRand(-100, 100);

b1 = fRand(-100, 100);
b2 = fRand(-100, 100);
b3 = fRand(-100, 100);
b4 = fRand(-100, 100);

Quaternion comp1{a1,a2,a3,a4};
Quaternion comp2{b1,b2,b3,b4};
quaternion<double> a(a1,a2,a3,a4);
quaternion<double> b(b1,b2,b3,b4);


//if (i%50000==0)
qDebug() << i;

compR = comp1+comp2;
boost = a+b;
if (!isEqual(compR, boost))
throw std::runtime_error("+");

compR = comp1-comp2;
boost = a-b;
if (!isEqual(compR, boost))
throw std::runtime_error("-");

compR = comp1*comp2;
boost = a*b;
if (!isEqual(compR, boost))
throw std::runtime_error("*");

compR = comp1/comp2;
boost = a/b;
if (!isEqual(compR, boost))
throw std::runtime_error("/");

}
} catch (const std::runtime_error& error) {
qDebug() << "Operation : [" << error.what() << "]";
qDebug() << " First vector = [" << a1 << " " << a2 << " " << " " << a3 << " " << a4 << "]\n" <<
"Second vector = [" << b1 << " " << b2 << " " << " " << b3 << " " << b4 << "]";
qDebug() << " My multiplication = [" << compR.real << " " << compR.imagine.data.at(0) << " " << compR.imagine.data.at(1)<< " " << compR.imagine.data.at(2) << "]\n" <<
"Boost multiplication = [" << boost.R_component_1() << " " << boost.R_component_2() << " " << boost.R_component_2() << " " << boost.R_component_3() << "]";
}

return 0;
}

完整项目: https://github.com/Yegres5/quaternion

最佳答案

您有两个问题:

  • 您在打印增强四元数时遇到错误(您两次打印了索引2)
  • 使用较大的epsilon。仅当数字约为1时才适合使用epsilon。结果约为10,000,因此您至少需要使用10,000x更大的epsilon。这可能还不够大,因为四元数乘法使用多个运算,每个运算都会增加一些其他误差。因此,为了安全起见,将ε再乘以10左右。
  • 关于c++ - 四元数乘法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60696329/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com