maths: move remaining operations into util namespace
This commit is contained in:
parent
5a26793b0a
commit
b1bc54ac8c
@ -82,7 +82,7 @@
|
||||
DEBUG_ONLY( \
|
||||
const auto __a = (A); \
|
||||
const auto __b = (B); \
|
||||
_CHECK_META (almost_equal (__a, __b), \
|
||||
_CHECK_META (util::almost_equal (__a, __b), \
|
||||
{ ; }, \
|
||||
{ \
|
||||
std::ostringstream __debug_os; \
|
||||
@ -203,7 +203,7 @@
|
||||
DEBUG_ONLY( \
|
||||
const auto __a = (A); \
|
||||
const auto __b = (B); \
|
||||
_CHECK_META (!almost_equal (__a, __b), \
|
||||
_CHECK_META (!util::almost_equal (__a, __b), \
|
||||
{ ; }, \
|
||||
{ \
|
||||
std::ostringstream __debug_neq_os; \
|
||||
|
@ -175,7 +175,7 @@ validate (json::tree::string &node,
|
||||
auto maxLength = schema.find ("maxLength");
|
||||
if (maxLength != schema.cend ()) {
|
||||
auto cmp = maxLength->second->as_number ().native ();
|
||||
if (!is_integer (cmp))
|
||||
if (!util::is_integer (cmp))
|
||||
throw length_error ("maxLength");
|
||||
|
||||
if (val.size () > cmp)
|
||||
@ -186,7 +186,7 @@ validate (json::tree::string &node,
|
||||
auto minLength = schema.find ("minLength");
|
||||
if (minLength != schema.cend ()) {
|
||||
auto cmp = minLength->second->as_number ().native ();
|
||||
if (!is_integer (cmp))
|
||||
if (!util::is_integer (cmp))
|
||||
throw length_error ("minLength");
|
||||
|
||||
if (val.size () < cmp)
|
||||
@ -217,7 +217,7 @@ validate (json::tree::number &node,
|
||||
if (mult != schema.cend ()) {
|
||||
auto div = mult->second->as_number ().native ();
|
||||
|
||||
if (val <= 0 || almost_equal (val, div))
|
||||
if (val <= 0 || util::almost_equal (val, div))
|
||||
throw json::schema_error ("multipleOf");
|
||||
}
|
||||
|
||||
|
@ -48,6 +48,7 @@ using json::tree::null;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util {
|
||||
template <>
|
||||
bool
|
||||
is_integer (const json::tree::number &node)
|
||||
@ -64,7 +65,7 @@ is_integer (const json::tree::node &node)
|
||||
return node.is_number () &&
|
||||
is_integer (node.as_number ());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static std::vector<json::flat::item>::const_iterator
|
||||
@ -305,7 +306,7 @@ size_t
|
||||
json::tree::node::as_uint (void) const
|
||||
{
|
||||
auto val = as_number ().native ();
|
||||
if (!is_integer (val))
|
||||
if (!util::is_integer (val))
|
||||
throw json::type_error ("cast fractional value to uint");
|
||||
|
||||
// TODO: use trunc_cast
|
||||
@ -761,7 +762,7 @@ json::tree::number::write (std::ostream &os) const
|
||||
//-----------------------------------------------------------------------------
|
||||
bool
|
||||
json::tree::number::operator ==(const json::tree::number &rhs) const
|
||||
{ return almost_equal (rhs.m_value, m_value); }
|
||||
{ return util::almost_equal (rhs.m_value, m_value); }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
67
maths.cpp
67
maths.cpp
@ -27,34 +27,36 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool
|
||||
is_pow2 (T value) {
|
||||
util::is_pow2 (T value)
|
||||
{
|
||||
typedef typename std::enable_if<std::is_integral<T>::value, bool>::type return_type;
|
||||
return (return_type)(value && !(value & (value - 1)));
|
||||
}
|
||||
|
||||
|
||||
template bool is_pow2 (uint8_t);
|
||||
template bool is_pow2 (uint16_t);
|
||||
template bool is_pow2 (uint32_t);
|
||||
template bool is_pow2 (uint64_t);
|
||||
template bool util::is_pow2 (uint8_t);
|
||||
template bool util::is_pow2 (uint16_t);
|
||||
template bool util::is_pow2 (uint32_t);
|
||||
template bool util::is_pow2 (uint64_t);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
T
|
||||
log2up (T v)
|
||||
util::log2up (T v)
|
||||
{
|
||||
return log2 ((v << 1) - 1);
|
||||
}
|
||||
|
||||
template uint32_t log2up (uint32_t);
|
||||
template uint64_t log2up (uint64_t);
|
||||
template uint32_t util::log2up (uint32_t);
|
||||
template uint64_t util::log2up (uint64_t);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
T
|
||||
log2 (T v) {
|
||||
util::log2 (T v)
|
||||
{
|
||||
static_assert (std::is_integral<T>::value,
|
||||
"log2 is only implemented for integers");
|
||||
|
||||
@ -65,28 +67,29 @@ log2 (T v) {
|
||||
return l;
|
||||
}
|
||||
|
||||
template uint8_t log2 (uint8_t);
|
||||
template uint16_t log2 (uint16_t);
|
||||
template uint32_t log2 (uint32_t);
|
||||
template uint64_t log2 (uint64_t);
|
||||
template uint8_t util::log2 (uint8_t);
|
||||
template uint16_t util::log2 (uint16_t);
|
||||
template uint32_t util::log2 (uint32_t);
|
||||
template uint64_t util::log2 (uint64_t);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
double
|
||||
rootsquare (T a, T b)
|
||||
util::rootsquare (T a, T b)
|
||||
{ return sqrt (util::pow2 (a) + util::pow2 (b)); }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template double rootsquare (double, double);
|
||||
template double rootsquare ( int, int);
|
||||
template double util::rootsquare (double, double);
|
||||
template double util::rootsquare ( int, int);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool
|
||||
is_integer (const T &value) {
|
||||
util::is_integer (const T &value)
|
||||
{
|
||||
T integer;
|
||||
return exactly_equal (std::modf (value, &integer),
|
||||
static_cast<T> (0.0));
|
||||
@ -94,14 +97,16 @@ is_integer (const T &value) {
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template bool is_integer (const double&);
|
||||
template bool is_integer (const float&);
|
||||
template bool util::is_integer (const double&);
|
||||
template bool util::is_integer (const float&);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util {
|
||||
template <>
|
||||
unsigned
|
||||
digits (const uint32_t &v) {
|
||||
digits (const uint32_t &v)
|
||||
{
|
||||
return (v >= 1000000000) ? 10 :
|
||||
(v >= 100000000) ? 9 :
|
||||
(v >= 10000000) ? 8 :
|
||||
@ -113,6 +118,7 @@ digits (const uint32_t &v) {
|
||||
(v >= 10) ? 2 :
|
||||
1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -120,7 +126,8 @@ template <typename T>
|
||||
std::enable_if_t<
|
||||
std::is_integral<T>::value, T
|
||||
>
|
||||
round_pow2 (T value) {
|
||||
util::round_pow2 (T value)
|
||||
{
|
||||
using return_type = std::enable_if_t<std::is_integral<T>::value, T>;
|
||||
|
||||
--value;
|
||||
@ -135,15 +142,15 @@ round_pow2 (T value) {
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template uint8_t round_pow2 (uint8_t);
|
||||
template uint16_t round_pow2 (uint16_t);
|
||||
template uint32_t round_pow2 (uint32_t);
|
||||
template uint64_t round_pow2 (uint64_t);
|
||||
template uint8_t util::round_pow2 (uint8_t);
|
||||
template uint16_t util::round_pow2 (uint16_t);
|
||||
template uint32_t util::round_pow2 (uint32_t);
|
||||
template uint64_t util::round_pow2 (uint64_t);
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template const float PI<float>;
|
||||
template const double PI<double>;
|
||||
template const float util::PI<float>;
|
||||
template const double util::PI<double>;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -151,7 +158,7 @@ template const double PI<double>;
|
||||
// so it's easier to instantiate early and check for broken code at library
|
||||
// build time.
|
||||
|
||||
template float limit (float, float, float);
|
||||
template float util::limit (float, float, float);
|
||||
|
||||
template float smoothstep (float, float, float);
|
||||
template double smoothstep (double, double, double);
|
||||
template float util::smoothstep (float, float, float);
|
||||
template double util::smoothstep (double, double, double);
|
||||
|
36
maths.hpp
36
maths.hpp
@ -44,31 +44,25 @@ namespace util {
|
||||
{
|
||||
return t > 0 ? t : -t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// exponentials
|
||||
|
||||
namespace util {
|
||||
template <typename T>
|
||||
constexpr T
|
||||
pow2 [[gnu::const]] (T value)
|
||||
{
|
||||
return value * value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
constexpr T
|
||||
pow [[gnu::const]] (T x, unsigned y);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool
|
||||
is_pow2 (T value);
|
||||
@ -143,7 +137,6 @@ template <typename T>
|
||||
unsigned
|
||||
digits (const T& value);
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
constexpr int sign (int);
|
||||
constexpr float sign (float);
|
||||
@ -361,9 +354,8 @@ fsum (InputIt first, InputIt last)
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// Variadic minimum
|
||||
namespace util {
|
||||
template <typename T>
|
||||
constexpr T
|
||||
min (const T a)
|
||||
@ -402,10 +394,9 @@ namespace util {
|
||||
{
|
||||
return max (a > b ? a : b, std::forward<Args> (args)...);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Limiting functions
|
||||
|
||||
// min/max clamping
|
||||
@ -421,7 +412,7 @@ limit (const T val, const U lo, const V hi)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
// clamped cubic hermite interpolation
|
||||
template <typename T>
|
||||
T
|
||||
@ -432,10 +423,9 @@ smoothstep (T a, T b, T x)
|
||||
return x * x * (3 - 2 * x);
|
||||
}
|
||||
|
||||
#include "types/string.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// renormalisation of unit floating point and/or normalised integers
|
||||
|
||||
// int -> float
|
||||
@ -450,7 +440,7 @@ renormalise (T t)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
// float -> int
|
||||
template <typename T, typename U>
|
||||
constexpr
|
||||
@ -483,7 +473,7 @@ renormalise (T t)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
// float -> float, avoid identity conversion as we don't want to create
|
||||
// ambiguous overloads
|
||||
template <typename T, typename U>
|
||||
@ -499,7 +489,7 @@ renormalise (T t)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
// hi_int -> lo_int
|
||||
template <typename T, typename U>
|
||||
constexpr
|
||||
@ -519,7 +509,7 @@ renormalise (T t)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
// lo_int -> hi_int
|
||||
template <typename T, typename U>
|
||||
constexpr
|
||||
@ -550,7 +540,7 @@ renormalise (T t)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename T, typename U>
|
||||
constexpr
|
||||
typename std::enable_if<
|
||||
@ -558,8 +548,10 @@ typename std::enable_if<
|
||||
>::type
|
||||
renormalise (T t)
|
||||
{ return t; }
|
||||
}
|
||||
|
||||
|
||||
//#include "types/string.hpp"
|
||||
#include "maths.ipp"
|
||||
|
||||
#endif // __MATHS_HPP
|
||||
|
@ -42,7 +42,7 @@ util::pow (T x, unsigned y)
|
||||
/// problems with constexpr under clang. If you need speed then you'll probably
|
||||
/// have to handcode something.
|
||||
constexpr int
|
||||
sign (int v)
|
||||
util::sign (int v)
|
||||
{
|
||||
return std::signbit (v) ? -1 : 1;
|
||||
}
|
||||
@ -50,7 +50,7 @@ sign (int v)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
constexpr float
|
||||
sign (float v)
|
||||
util::sign (float v)
|
||||
{
|
||||
return std::signbit (v) ? -1.f : 1.f;
|
||||
}
|
||||
@ -58,7 +58,7 @@ sign (float v)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
constexpr double
|
||||
sign (double v)
|
||||
util::sign (double v)
|
||||
{
|
||||
return std::signbit (v) ? -1. : 1.f;
|
||||
}
|
||||
|
@ -26,11 +26,11 @@ main (int, char**)
|
||||
{
|
||||
// white: hue is undefined
|
||||
auto white = util::rgb_to_hsv ({1,1,1});
|
||||
tap.expect (exactly_equal (white.s, 0) && exactly_equal (white.v, 1), "white hsv");
|
||||
tap.expect (util::exactly_zero (white.s) && util::exactly_equal (white.v, 1), "white hsv");
|
||||
|
||||
// black: hue is undefined
|
||||
auto black = util::rgb_to_hsv ({0,0,0});
|
||||
tap.expect (exactly_equal (black.s, 0) && exactly_equal (black.v, 0), "black hsv");
|
||||
tap.expect (util::exactly_zero (black.s) && util::exactly_zero (black.v), "black hsv");
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "fixed.hpp"
|
||||
#include "types/string.hpp"
|
||||
|
||||
#include "tap.hpp"
|
||||
|
||||
|
@ -46,8 +46,8 @@ int main ()
|
||||
foo d_foo { 7, 42.0 };
|
||||
auto f_tuple = util::as_tuple (d_foo);
|
||||
|
||||
tap.expect (exactly_equal (d_foo.a, std::get<0> (f_tuple)) &&
|
||||
exactly_equal (d_foo.b, std::get<1> (f_tuple)),
|
||||
tap.expect (util::exactly_equal (d_foo.a, std::get<0> (f_tuple)) &&
|
||||
util::exactly_equal (d_foo.b, std::get<1> (f_tuple)),
|
||||
"dynamic member access after conversion to tuple");
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ main (void) {
|
||||
CHECK (!ref["integer"].is_object ());
|
||||
CHECK (!ref["integer"].is_string ());
|
||||
CHECK (
|
||||
exactly_equal (
|
||||
util::exactly_equal (
|
||||
(unsigned)ref["integer"].as_number ().native (),
|
||||
1u
|
||||
)
|
||||
@ -81,7 +81,7 @@ main (void) {
|
||||
CHECK (!ref["double"].is_object ());
|
||||
CHECK (!ref["double"].is_string ());
|
||||
CHECK (
|
||||
exactly_equal (
|
||||
util::exactly_equal (
|
||||
ref["double"].as_number ().native (),
|
||||
3.14
|
||||
)
|
||||
|
@ -15,49 +15,49 @@ void
|
||||
test_comparisons (util::TAP::logger &tap)
|
||||
{
|
||||
// Check pos/neg zeroes
|
||||
tap.expect (almost_equal ( 0.f, 0.f), "equal float zeros +ve/+ve");
|
||||
tap.expect (almost_equal ( 0.f, -0.f), "equal float zeros +ve/-ve");
|
||||
tap.expect (almost_equal (-0.f, 0.f), "equal float zeros -ve/+ve");
|
||||
tap.expect (almost_equal (-0.f, -0.f), "equal float zeros -ve/-ve");
|
||||
tap.expect (util::almost_equal ( 0.f, 0.f), "equal float zeros +ve/+ve");
|
||||
tap.expect (util::almost_equal ( 0.f, -0.f), "equal float zeros +ve/-ve");
|
||||
tap.expect (util::almost_equal (-0.f, 0.f), "equal float zeros -ve/+ve");
|
||||
tap.expect (util::almost_equal (-0.f, -0.f), "equal float zeros -ve/-ve");
|
||||
|
||||
tap.expect (almost_equal ( 0., 0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (almost_equal ( 0., -0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (almost_equal (-0., 0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (almost_equal (-0., -0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (util::almost_equal ( 0., 0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (util::almost_equal ( 0., -0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (util::almost_equal (-0., 0.), "equal double zeroes +ve/+ve");
|
||||
tap.expect (util::almost_equal (-0., -0.), "equal double zeroes +ve/+ve");
|
||||
|
||||
// Check zero comparison with values near the expected cutoff
|
||||
tap.expect (almost_zero (1e-45f), "almost_zero with low value");
|
||||
tap.expect (!almost_zero (1e-40f), "not almost_zero with low value");
|
||||
tap.expect (!exactly_zero (1e-45f), "not exactly_zero with low value");
|
||||
tap.expect (util::almost_zero (1e-45f), "almost_zero with low value");
|
||||
tap.expect (!util::almost_zero (1e-40f), "not almost_zero with low value");
|
||||
tap.expect (!util::exactly_zero (1e-45f), "not exactly_zero with low value");
|
||||
|
||||
// Compare values a little away from zero
|
||||
tap.expect (!almost_equal (-2.0, 0.0), "not equal floats");
|
||||
tap.expect (!almost_equal (-2.f, 0.f), "not equal doubles");
|
||||
tap.expect (!util::almost_equal (-2.0, 0.0), "not equal floats");
|
||||
tap.expect (!util::almost_equal (-2.f, 0.f), "not equal doubles");
|
||||
|
||||
// Compare values at the maximum extreme
|
||||
tap.expect (!almost_equal (-std::numeric_limits<float>::max (), 0.f), "not equal -max/0 float");
|
||||
tap.expect (!almost_equal (-std::numeric_limits<float>::max (),
|
||||
tap.expect (!util::almost_equal (-std::numeric_limits<float>::max (), 0.f), "not equal -max/0 float");
|
||||
tap.expect (!util::almost_equal (-std::numeric_limits<float>::max (),
|
||||
std::numeric_limits<float>::max ()),
|
||||
"not equal -max/max");
|
||||
|
||||
// Compare infinity values
|
||||
tap.expect ( almost_equal (numeric_limits<double>::infinity (),
|
||||
tap.expect ( util::almost_equal (numeric_limits<double>::infinity (),
|
||||
numeric_limits<double>::infinity ()),
|
||||
"almost_equal +infinity");
|
||||
tap.expect (!almost_equal (numeric_limits<double>::infinity (), 0.0),
|
||||
tap.expect (!util::almost_equal (numeric_limits<double>::infinity (), 0.0),
|
||||
"not almost_equal +inf/0");
|
||||
|
||||
// Compare NaNs
|
||||
tap.expect (!almost_equal (0., numeric_limits<double>::quiet_NaN ()), "not almost_equal double 0/NaN");
|
||||
tap.expect (!almost_equal (numeric_limits<double>::quiet_NaN (), 0.), "not almost_equal double NaN/0");
|
||||
tap.expect (!util::almost_equal (0., numeric_limits<double>::quiet_NaN ()), "not almost_equal double 0/NaN");
|
||||
tap.expect (!util::almost_equal (numeric_limits<double>::quiet_NaN (), 0.), "not almost_equal double NaN/0");
|
||||
|
||||
tap.expect (!almost_equal (numeric_limits<double>::quiet_NaN (),
|
||||
tap.expect (!util::almost_equal (numeric_limits<double>::quiet_NaN (),
|
||||
numeric_limits<double>::quiet_NaN ()),
|
||||
"not almost_equal NaN/NaN");
|
||||
|
||||
// Compare reasonably close values that are wrong
|
||||
tap.expect (!almost_equal (1.0000f, 1.0001f), ".0001f difference inequality");
|
||||
tap.expect ( almost_equal (1.0000f, 1.00001f), ".00001f difference inequality");
|
||||
tap.expect (!util::almost_equal (1.0000f, 1.0001f), ".0001f difference inequality");
|
||||
tap.expect ( util::almost_equal (1.0000f, 1.00001f), ".00001f difference inequality");
|
||||
}
|
||||
|
||||
|
||||
@ -66,10 +66,10 @@ test_normalisations (util::TAP::logger &tap)
|
||||
{
|
||||
// u8 to float
|
||||
{
|
||||
auto a = renormalise<uint8_t,float> (255);
|
||||
auto a = util::renormalise<uint8_t,float> (255);
|
||||
tap.expect_eq (a, 1.f, "normalise uint8 max");
|
||||
|
||||
auto b = renormalise<uint8_t,float> (0);
|
||||
auto b = util::renormalise<uint8_t,float> (0);
|
||||
tap.expect_eq (b, 0.f, "normalise uint8 min");
|
||||
}
|
||||
|
||||
@ -88,8 +88,8 @@ test_normalisations (util::TAP::logger &tap)
|
||||
};
|
||||
|
||||
for (auto i: TESTS) {
|
||||
auto v = renormalise<decltype(i.a),decltype(i.b)> (i.a);
|
||||
success = success && almost_equal (unsigned{v}, unsigned{i.b});
|
||||
auto v = util::renormalise<decltype(i.a),decltype(i.b)> (i.a);
|
||||
success = success && util::almost_equal (unsigned{v}, unsigned{i.b});
|
||||
}
|
||||
|
||||
tap.expect (success, "float-u8 normalisation");
|
||||
@ -111,17 +111,17 @@ test_normalisations (util::TAP::logger &tap)
|
||||
};
|
||||
|
||||
for (auto t: TESTS) {
|
||||
auto v = renormalise<float,uint32_t> (t.a);
|
||||
success = success && almost_equal (t.b, v);
|
||||
auto v = util::renormalise<float,uint32_t> (t.a);
|
||||
success = success && util::almost_equal (t.b, v);
|
||||
}
|
||||
|
||||
tap.expect (success, "float-u32 normalisation");
|
||||
}
|
||||
|
||||
tap.expect_eq (renormalise<uint8_t,uint32_t> (0xff), 0xffffffffu, "normalise hi u8-to-u32");
|
||||
tap.expect_eq (renormalise<uint8_t,uint32_t> (0x00), 0x00000000u, "normalise lo u8-to-u32");
|
||||
tap.expect_eq (util::renormalise<uint8_t,uint32_t> (0xff), 0xffffffffu, "normalise hi u8-to-u32");
|
||||
tap.expect_eq (util::renormalise<uint8_t,uint32_t> (0x00), 0x00000000u, "normalise lo u8-to-u32");
|
||||
|
||||
tap.expect_eq (renormalise<uint32_t,uint8_t> (0xffffffff), 0xffu, "normalise hi u32-to-u8");
|
||||
tap.expect_eq (util::renormalise<uint32_t,uint8_t> (0xffffffff), 0xffu, "normalise hi u32-to-u8");
|
||||
}
|
||||
|
||||
|
||||
@ -143,30 +143,30 @@ main (void)
|
||||
|
||||
tap.expect_eq (util::pow2 (4u), 16u, "pow2");
|
||||
|
||||
tap.expect_eq (rootsquare (2, 2), sqrt (8), "rootsquare");
|
||||
tap.expect_eq (util::rootsquare (2, 2), sqrt (8), "rootsquare");
|
||||
|
||||
static const double POS_ZERO = 1.0 / numeric_limits<double>::infinity ();
|
||||
static const double NEG_ZERO = -1.0 / numeric_limits<double>::infinity ();
|
||||
|
||||
tap.expect_eq (sign (-1), -1, "sign(-1)");
|
||||
tap.expect_eq (sign ( 1), 1, "sign( 1)");
|
||||
tap.expect_eq (sign (POS_ZERO), 1., "sign (POS_ZERO)");
|
||||
tap.expect_eq (sign (NEG_ZERO), -1., "sign (NEG_ZERO)");
|
||||
tap.expect_eq (sign ( numeric_limits<double>::infinity ()), 1., "sign +inf");
|
||||
tap.expect_eq (sign (-numeric_limits<double>::infinity ()), -1., "sign -inf");
|
||||
tap.expect_eq (util::sign (-1), -1, "sign(-1)");
|
||||
tap.expect_eq (util::sign ( 1), 1, "sign( 1)");
|
||||
tap.expect_eq (util::sign (POS_ZERO), 1., "sign (POS_ZERO)");
|
||||
tap.expect_eq (util::sign (NEG_ZERO), -1., "sign (NEG_ZERO)");
|
||||
tap.expect_eq (util::sign ( numeric_limits<double>::infinity ()), 1., "sign +inf");
|
||||
tap.expect_eq (util::sign (-numeric_limits<double>::infinity ()), -1., "sign -inf");
|
||||
|
||||
tap.expect_eq (to_degrees (PI< float>), 180.f, "to_degrees float");
|
||||
tap.expect_eq (to_degrees (PI<double>), 180.0, "to_degrees double");
|
||||
tap.expect_eq (to_radians (180.f), PI<float>, "to_radians float");
|
||||
tap.expect_eq (to_radians (180.0), PI<double>, "to_radians double");
|
||||
tap.expect_eq (util::to_degrees (util::PI< float>), 180.f, "to_degrees float");
|
||||
tap.expect_eq (util::to_degrees (util::PI<double>), 180.0, "to_degrees double");
|
||||
tap.expect_eq (util::to_radians (180.f), util::PI<float>, "to_radians float");
|
||||
tap.expect_eq (util::to_radians (180.0), util::PI<double>, "to_radians double");
|
||||
|
||||
tap.expect_eq (log2 (8u), 3u, "log2 +ve");
|
||||
tap.expect_eq (log2 (1u), 0u, "log2 zero");
|
||||
tap.expect_eq (util::log2 (8u), 3u, "log2 +ve");
|
||||
tap.expect_eq (util::log2 (1u), 0u, "log2 zero");
|
||||
|
||||
//tap.expect_eq (log2 (9u), 3, "log2up 9");
|
||||
tap.expect_eq (log2up (9u), 4u, "log2up 9");
|
||||
tap.expect_eq (log2up (8u), 3u, "log2up 9");
|
||||
tap.expect_eq (log2up (1u), 0u, "log2up 9");
|
||||
tap.expect_eq (util::log2up (9u), 4u, "log2up 9");
|
||||
tap.expect_eq (util::log2up (8u), 3u, "log2up 9");
|
||||
tap.expect_eq (util::log2up (1u), 0u, "log2up 9");
|
||||
|
||||
return tap.status ();
|
||||
}
|
||||
|
@ -32,10 +32,10 @@ main (void)
|
||||
auto r = m * v;
|
||||
|
||||
tap.expect (
|
||||
almost_equal (r.x, 30.f) &&
|
||||
almost_equal (r.y, 70.f) &&
|
||||
almost_equal (r.z, 110.f) &&
|
||||
almost_equal (r.w, 150.f),
|
||||
util::almost_equal (r.x, 30.f) &&
|
||||
util::almost_equal (r.y, 70.f) &&
|
||||
util::almost_equal (r.z, 110.f) &&
|
||||
util::almost_equal (r.w, 150.f),
|
||||
"simple matrix-vector multiplication"
|
||||
);
|
||||
}
|
||||
@ -79,9 +79,9 @@ main (void)
|
||||
for (size_t r = 0; r < m.rows; ++r)
|
||||
for (size_t c = 0; c < m.cols; ++c)
|
||||
if (r == c)
|
||||
success = success && almost_equal (m.values[r][c], 1.f);
|
||||
success = success && util::almost_equal (m.values[r][c], 1.f);
|
||||
else
|
||||
success = success && almost_equal (m.values[r][c], 0.f);
|
||||
success = success && util::almost_equal (m.values[r][c], 0.f);
|
||||
|
||||
tap.expect (success, "identity inversion");
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ main (int, char**)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!almost_equal (i.solutions[j], s[j]))
|
||||
if (!util::almost_equal (i.solutions[j], s[j]))
|
||||
ok = false;
|
||||
}
|
||||
|
||||
|
@ -28,13 +28,13 @@ test_polar (util::TAP::logger &tap)
|
||||
},
|
||||
|
||||
{
|
||||
{ 1.f, PI<float> / 2.f },
|
||||
{ 1.f, util::PI<float> / 2.f },
|
||||
{ 0.f, 1.f },
|
||||
"unit length, rotated"
|
||||
},
|
||||
|
||||
{
|
||||
{ 1.f, 2 * PI<float> },
|
||||
{ 1.f, 2 * util::PI<float> },
|
||||
{ 1.f, 0.f },
|
||||
"full rotation, unit length"
|
||||
}
|
||||
@ -53,8 +53,8 @@ test_polar (util::TAP::logger &tap)
|
||||
auto in_polar = t.polar;
|
||||
auto to_polar = util::cartesian_to_polar (t.cartesian);
|
||||
|
||||
in_polar[1] = std::fmod (in_polar[1], 2 * PI<float>);
|
||||
to_polar[1] = std::fmod (to_polar[1], 2 * PI<float>);
|
||||
in_polar[1] = std::fmod (in_polar[1], 2 * util::PI<float>);
|
||||
to_polar[1] = std::fmod (to_polar[1], 2 * util::PI<float>);
|
||||
|
||||
tap.expect_eq (in_polar, to_polar, t.desc);
|
||||
}
|
||||
@ -83,7 +83,7 @@ test_euler (util::TAP::logger &tap)
|
||||
// check that simple axis rotations look correct
|
||||
for (auto i: TESTS) {
|
||||
tap.expect_eq (util::to_euler (i.dir),
|
||||
i.euler * PI<float>,
|
||||
i.euler * util::PI<float>,
|
||||
"to euler, %s", i.name);
|
||||
}
|
||||
|
||||
|
@ -319,6 +319,8 @@ namespace util {
|
||||
template <> vector<4,double> random (void) { util::vector<4,double> out; randomise (out.data); return out; }
|
||||
}
|
||||
|
||||
|
||||
namespace util {
|
||||
template <>
|
||||
bool
|
||||
almost_equal [[gnu::pure]] (const util::vector2f &a, const util::vector2f &b)
|
||||
@ -326,4 +328,5 @@ almost_equal [[gnu::pure]] (const util::vector2f &a, const util::vector2f &b)
|
||||
bool (*comparator) (const float&, const float&) = almost_equal;
|
||||
return std::equal (a.begin (), a.end (), b.begin (), comparator);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user