Skip to content

Commit

Permalink
tracking precision difference between Debug and Release builds in qua…
Browse files Browse the repository at this point in the history
…d-double
  • Loading branch information
Ravenwater committed Aug 31, 2024
1 parent ad24851 commit 99e5b49
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
6 changes: 3 additions & 3 deletions include/universal/number/qd/mathext/horners.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace sw { namespace universal {

for (int i = n - 1; i >= 0; --i) {
r *= x;
r += coefficients[i];
r += coefficients[static_cast<unsigned>(i)];
}

return r;
Expand All @@ -43,14 +43,14 @@ namespace sw { namespace universal {
inline qd polyroot(const std::vector<qd>& c, const qd& x0, int max_iter, double threshold = 1e-32) {
if (threshold == 0.0) threshold = qd_eps;

int n = c.size() - 1;
int n = static_cast<int>(c.size()) - 1;
double max_c = std::abs(double(c[0]));
// Compute the coefficients of the derivatives
std::vector<qd> derivatives{ c };
for (int i = 1; i <= n; ++i) {
double v = std::abs(double(c[static_cast<unsigned>(i)]));
if (v > max_c) max_c = v;
derivatives[i - 1] = c[static_cast<unsigned>(i)] * static_cast<double>(i);
derivatives[static_cast<unsigned>(i - 1)] = c[static_cast<unsigned>(i)] * static_cast<double>(i);
}
threshold *= max_c;

Expand Down
49 changes: 47 additions & 2 deletions static/qd/api/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,62 @@ qd_1_sqrt2 : 7.07106781186547524400844362104849e-01 vs 7.071067811865475244
{

qd a{ 2.0 };
std::cout << "sqrt(2.0) " << to_quad(sqrt(a)) << '\n';
std::cout << "1/sqrt(2.0) " << to_quad(reciprocal(sqrt(a))) << '\n';
qd sqrt2 = sqrt(a);
std::cout << "sqrt(2.0) " << to_quad(sqrt2) << '\n';

a = 3.0;
std::cout << "sqrt(3.0) " << to_quad(sqrt(a)) << '\n';
a = 5.0;
std::cout << "sqrt(5.0) " << to_quad(sqrt(a)) << '\n';

std::cout << "1/sqrt(2.0) " << to_quad(reciprocal(sqrt2)) << '\n';

a = sqrt(qd_pi);
std::cout << "2 / sqrtpi " << to_quad(2.0 / a) << '\n';
}
/*
*
Debug build
sqrt(2.0) ( 1.4142135623730951, -9.6672933134529135e-17, 4.1386753086994136e-33, 4.9355469914683509e-50)
sqrt(3.0) ( 1.7320508075688772, 1.0035084221806903e-16, -1.4959542475733896e-33, 5.3061475632961675e-50)
sqrt(5.0) ( 2.2360679774997898, -1.0864230407365012e-16, 5.3086504167631309e-33, -6.6099839950042175e-50)
1/sqrt(2.0) ( 0.70710678118654757, -4.8336466567264567e-17, 2.0693376543497068e-33, 2.4677734957341755e-50)
2 / sqrtpi ( 1.1283791670955126, 1.5335459613165881e-17, -4.7656845966936863e-34, -2.0077946616552625e-50)
Release build
sqrt(2.0) ( 1.4142135623730951, -9.6672933134529135e-17, 4.1386753203466335e-33, -3.3032885712977947e-49)
sqrt(3.0) ( 1.7320508075688772, 1.0035084221806903e-16, -1.4959542883445281e-33, 5.0676801879243325e-50)
sqrt(5.0) ( 2.2360679774997898, -1.0864230407365012e-16, 5.3086504310320564e-33, -2.7103246582355688e-49)
1/sqrt(2.0) ( 0.70710678118654757, -4.8336466312625432e-17, -3.039266735626984e-34, -1.350504809842679e-50)
2 / sqrtpi ( 1.1283791670955126, 1.5335458971746789e-17, 2.6579683555126638e-34, -1.683757146154259e-50)
difference sqrt2 : -1.16472195516512003859185071422508e-41
difference sqrt3 : +4.07711385546630871610406778813869e-41
difference sqrt5 : -1.42689253079290274645876254577245e-41
difference 1_sqrt2 : -2.54639133258339414062837196323682e-25
difference 2_sqrtpi : +6.41419091181394536188494115009207e-25
*/

{
qd debug_sqrt2(1.4142135623730951, -9.6672933134529135e-17, 4.1386753086994136e-33, 4.9355469914683509e-50);
qd debug_sqrt3 (1.7320508075688772, 1.0035084221806903e-16, -1.4959542475733896e-33, 5.3061475632961675e-50);
qd debug_sqrt5(2.2360679774997898, -1.0864230407365012e-16, 5.3086504167631309e-33, -6.6099839950042175e-50);
qd debug_1_sqrt2(0.70710678118654757, -4.8336466567264567e-17, 2.0693376543497068e-33, 2.4677734957341755e-50);
qd debug_2_sqrtpi(1.1283791670955126, 1.5335459613165881e-17, -4.7656845966936863e-34, -2.0077946616552625e-50);

qd release_sqrt2(1.4142135623730951, -9.6672933134529135e-17, 4.1386753203466335e-33, -3.3032885712977947e-49);
qd release_sqrt3(1.7320508075688772, 1.0035084221806903e-16, -1.4959542883445281e-33, 5.0676801879243325e-50);
qd release_sqrt5(2.2360679774997898, -1.0864230407365012e-16, 5.3086504310320564e-33, -2.7103246582355688e-49);
qd release_1_sqrt2(0.70710678118654757, -4.8336466312625432e-17, -3.039266735626984e-34, -1.350504809842679e-50);
qd release_2_sqrtpi(1.1283791670955126, 1.5335458971746789e-17, 2.6579683555126638e-34, -1.683757146154259e-50);

std::cout << "difference sqrt2 : " << (debug_sqrt2 - release_sqrt2) << '\n';
std::cout << "difference sqrt3 : " << (debug_sqrt3 - release_sqrt3) << '\n';
std::cout << "difference sqrt5 : " << (debug_sqrt5 - release_sqrt5) << '\n';
std::cout << "difference 1_sqrt2 : " << (debug_1_sqrt2 - release_1_sqrt2) << '\n';
std::cout << "difference 2_sqrtpi : " << (debug_2_sqrtpi - release_2_sqrtpi) << '\n';
}

std::cout << std::setprecision(oldPrec);

Expand Down

0 comments on commit 99e5b49

Please sign in to comment.