[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master c6dc35ea 05/11: Demonstrate equivalence of bi
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master c6dc35ea 05/11: Demonstrate equivalence of bin_exp() to SGI's power() |
Date: |
Tue, 31 May 2022 17:52:05 -0400 (EDT) |
branch: master
commit c6dc35eaa5f6f7602105fcf05d182f3b34f5de8f
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Demonstrate equivalence of bin_exp() to SGI's power()
Hid everything except the two-argument (multiplicative) power(), to
show that nothing else is used. Replaced argument types for the few
cases where they clash with bin_exp(). Asserted equality of each
result.
---
stl_extensions.hpp | 34 ++++++++++++++++++++++++++++++++--
1 file changed, 32 insertions(+), 2 deletions(-)
diff --git a/stl_extensions.hpp b/stl_extensions.hpp
index 813820f7..a360c792 100644
--- a/stl_extensions.hpp
+++ b/stl_extensions.hpp
@@ -53,11 +53,17 @@
#include "config.hpp"
+#include "assert_lmi.hpp"
+#include "bin_exp.hpp"
+#include "bourn_cast.hpp"
+#include "miscellany.hpp" // scoped_ios_format
+
#include <functional> // multiplies, plus
+#include <iostream>
#include <stdexcept> // logic_error
#include <type_traits> // is_integral_v
-namespace nonstd
+namespace hidden
{
/// Identity element.
@@ -115,11 +121,35 @@ T power(T x, Integer n, MonoidOperation opr)
return result;
}
}
+} // namespace hidden
+namespace nonstd
+{
template <typename T, typename Integer>
inline T power(T x, Integer n)
{
- return power(x, n, std::multiplies<T>());
+ using F = std::conditional_t<std::is_floating_point_v<T>, T, double>;
+
+ T const r = hidden::power(x, n, std::multiplies<T>());
+ F const a = bourn_cast<F>(r);
+ F const b = bin_exp(bourn_cast<F>(x), bourn_cast<int>(n));
+ if(a != b)
+ {
+ scoped_ios_format meaningless_name(std::cout);
+ std::cout.precision(32);
+ std::cout
+ << "power <> bin_exp:\n"
+ << x << " x\n"
+ << n << " n\n"
+ << a << " a\n"
+ << b << " b\n"
+ << a - b << " a - b\n"
+ << (a - b) / a << " (a - b) / a\n"
+ << std::endl
+ ;
+ }
+ LMI_ASSERT(a == b);
+ return r;
}
} // namespace nonstd
- [lmi-commits] [lmi] master updated (44ea8a5b -> fd325f5e), Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master d36e5145 03/11: Suppress undefined behavior; add a test with defined behavior, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 28055a62 02/11: Realize the actual intention of a comparison, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master a3278e2b 10/11: Expunge 'stl_extensions.hpp', Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master c6dc35ea 05/11: Demonstrate equivalence of bin_exp() to SGI's power(),
Greg Chicares <=
- [lmi-commits] [lmi] master 8922e7a1 06/11: Explain restrictions on bin_exp() argument types, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 8cbc3001 07/11: Make a value definitely exact and definitely constexpr, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 2c8cc917 04/11: Don't bother calculating 2^64 - 1, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 7727d36d 08/11: Prefer bin_exp() to SGI's power(), Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master d9655b77 01/11: Refactor: rename local variables, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 2576d70c 09/11: Prefer bin_exp() to SGI's power(), Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master fd325f5e 11/11: Record speed measurements, Greg Chicares, 2022/05/31