2015-04-13 16:45:56 +10:00
|
|
|
#include "maths.hpp"
|
|
|
|
|
|
|
|
#include "tap.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cmath>
|
2012-05-03 15:59:54 +10:00
|
|
|
#include <limits>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
using std::sqrt;
|
2012-05-03 15:59:54 +10:00
|
|
|
using std::numeric_limits;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-01-21 23:39:23 +11:00
|
|
|
|
2015-02-03 15:54:55 +11:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_comparisons (cruft::TAP::logger &tap)
|
2015-02-03 15:54:55 +11:00
|
|
|
{
|
|
|
|
// Check pos/neg zeroes
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (cruft::almost_equal ( 0.f, 0.f), "equal float zeros +ve/+ve");
|
|
|
|
tap.expect (cruft::almost_equal ( 0.f, -0.f), "equal float zeros +ve/-ve");
|
|
|
|
tap.expect (cruft::almost_equal (-0.f, 0.f), "equal float zeros -ve/+ve");
|
|
|
|
tap.expect (cruft::almost_equal (-0.f, -0.f), "equal float zeros -ve/-ve");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (cruft::almost_equal ( 0., 0.), "equal double zeroes +ve/+ve");
|
|
|
|
tap.expect (cruft::almost_equal ( 0., -0.), "equal double zeroes +ve/+ve");
|
|
|
|
tap.expect (cruft::almost_equal (-0., 0.), "equal double zeroes +ve/+ve");
|
|
|
|
tap.expect (cruft::almost_equal (-0., -0.), "equal double zeroes +ve/+ve");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
|
|
|
// Check zero comparison with values near the expected cutoff
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (cruft::almost_zero (1e-45f), "almost_zero with low value");
|
|
|
|
tap.expect (!cruft::almost_zero (1e-40f), "not almost_zero with low value");
|
|
|
|
tap.expect (!cruft::exactly_zero (1e-45f), "not exactly_zero with low value");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
|
|
|
// Compare values a little away from zero
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!cruft::almost_equal (-2.0, 0.0), "not equal floats");
|
|
|
|
tap.expect (!cruft::almost_equal (-2.f, 0.f), "not equal doubles");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
|
|
|
// Compare values at the maximum extreme
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!cruft::almost_equal (-std::numeric_limits<float>::max (), 0.f), "not equal -max/0 float");
|
|
|
|
tap.expect (!cruft::almost_equal (-std::numeric_limits<float>::max (),
|
2015-11-16 11:42:20 +11:00
|
|
|
std::numeric_limits<float>::max ()),
|
2015-07-13 16:29:01 +10:00
|
|
|
"not equal -max/max");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
|
|
|
// Compare infinity values
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect ( cruft::almost_equal (numeric_limits<double>::infinity (),
|
2015-11-16 11:42:20 +11:00
|
|
|
numeric_limits<double>::infinity ()),
|
2015-07-13 16:29:01 +10:00
|
|
|
"almost_equal +infinity");
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!cruft::almost_equal (numeric_limits<double>::infinity (), 0.0),
|
2015-07-13 16:29:01 +10:00
|
|
|
"not almost_equal +inf/0");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
|
|
|
// Compare NaNs
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!cruft::almost_equal (0., numeric_limits<double>::quiet_NaN ()), "not almost_equal double 0/NaN");
|
|
|
|
tap.expect (!cruft::almost_equal (numeric_limits<double>::quiet_NaN (), 0.), "not almost_equal double NaN/0");
|
2015-02-03 15:54:55 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!cruft::almost_equal (numeric_limits<double>::quiet_NaN (),
|
2015-11-16 11:42:20 +11:00
|
|
|
numeric_limits<double>::quiet_NaN ()),
|
2015-07-13 16:29:01 +10:00
|
|
|
"not almost_equal NaN/NaN");
|
2015-08-20 15:34:20 +10:00
|
|
|
|
|
|
|
// Compare reasonably close values that are wrong
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (!cruft::almost_equal (1.0000f, 1.0001f), ".0001f difference inequality");
|
|
|
|
tap.expect ( cruft::almost_equal (1.0000f, 1.00001f), ".00001f difference inequality");
|
2015-02-03 15:54:55 +11:00
|
|
|
}
|
2012-05-03 15:59:54 +10:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-04-09 17:44:50 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_normalisations (cruft::TAP::logger &tap)
|
2015-04-09 17:44:50 +10:00
|
|
|
{
|
|
|
|
// u8 to float
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
auto a = cruft::renormalise<uint8_t,float> (255);
|
2015-07-13 16:29:01 +10:00
|
|
|
tap.expect_eq (a, 1.f, "normalise uint8 max");
|
2015-04-09 17:44:50 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto b = cruft::renormalise<uint8_t,float> (0);
|
2015-07-13 16:29:01 +10:00
|
|
|
tap.expect_eq (b, 0.f, "normalise uint8 min");
|
2015-04-09 17:44:50 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// float to u8
|
|
|
|
{
|
2015-07-13 16:29:01 +10:00
|
|
|
bool success = true;
|
|
|
|
|
2015-10-20 16:50:44 +11:00
|
|
|
static const struct {
|
2015-04-09 17:44:50 +10:00
|
|
|
float a;
|
|
|
|
uint8_t b;
|
|
|
|
} TESTS[] = {
|
2015-10-20 16:50:44 +11:00
|
|
|
{ 1.f, 255 },
|
|
|
|
{ 0.5f, 127 },
|
|
|
|
{ 2.f, 255 },
|
|
|
|
{ -1.f, 0 }
|
2015-04-09 17:44:50 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto i: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
auto v = cruft::renormalise<decltype(i.a),decltype(i.b)> (i.a);
|
|
|
|
success = success && cruft::almost_equal (unsigned{v}, unsigned{i.b});
|
2015-04-09 17:44:50 +10:00
|
|
|
}
|
2015-07-13 16:29:01 +10:00
|
|
|
|
|
|
|
tap.expect (success, "float-u8 normalisation");
|
2015-04-09 17:44:50 +10:00
|
|
|
}
|
2015-10-20 16:50:44 +11:00
|
|
|
|
|
|
|
// float to uint32
|
|
|
|
// exercises an integer type that has more precision than float
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
float a;
|
|
|
|
uint32_t b;
|
|
|
|
} TESTS[] {
|
|
|
|
{ 0.f, 0x00000000 }, // lo range
|
|
|
|
{ 1.f, 0xffffffff }, // hi range
|
|
|
|
{ 0.5f, 0x7fffff7f }, // 31 bits
|
|
|
|
{ 0.001953125f, 0x007fff00 }, // 23 bits
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto t: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
auto v = cruft::renormalise<float,uint32_t> (t.a);
|
|
|
|
success = success && cruft::almost_equal (t.b, v);
|
2015-10-20 16:50:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
tap.expect (success, "float-u32 normalisation");
|
|
|
|
}
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::renormalise<uint8_t,uint32_t> (0xff), 0xffffffffu, "normalise hi u8-to-u32");
|
|
|
|
tap.expect_eq (cruft::renormalise<uint8_t,uint32_t> (0x00), 0x00000000u, "normalise lo u8-to-u32");
|
2015-10-20 16:50:44 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::renormalise<uint32_t,uint8_t> (0xffffffff), 0xffu, "normalise hi u32-to-u8");
|
2017-05-24 15:15:25 +10:00
|
|
|
|
|
|
|
// sint32 to uint32
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
int32_t sint;
|
|
|
|
uint32_t uint;
|
|
|
|
const char *msg;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ std::numeric_limits<int32_t>::min (), std::numeric_limits<uint32_t>::min (), "min" },
|
|
|
|
{ std::numeric_limits<int32_t>::max (), std::numeric_limits<uint32_t>::max (), "max" },
|
|
|
|
{ 0, std::numeric_limits<uint32_t>::max () / 2u + 1, "mid" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::renormalise<int32_t,uint32_t> (t.sint), t.uint, "%s s32-to-u32", t.msg);
|
|
|
|
tap.expect_eq (cruft::renormalise<uint32_t,int32_t> (t.uint), t.sint, "%s u32-to-s32", t.msg);
|
2017-05-24 15:15:25 +10:00
|
|
|
}
|
|
|
|
}
|
2015-04-09 17:44:50 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-02-03 15:54:55 +11:00
|
|
|
int
|
2015-07-13 16:29:01 +10:00
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2015-07-13 16:29:01 +10:00
|
|
|
|
2015-02-03 15:54:55 +11:00
|
|
|
// Max out the precision in case we trigger debug output
|
|
|
|
std::cerr.precision (std::numeric_limits<double>::digits10);
|
|
|
|
std::cout.precision (std::numeric_limits<double>::digits10);
|
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
test_comparisons (tap);
|
2015-01-29 15:47:59 +11:00
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
test_normalisations (tap);
|
2015-04-09 17:44:50 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::min (-2, 0, 2), -2, "variadic min");
|
|
|
|
tap.expect_eq (cruft::max (-2, 0, 2), 2, "variadic max");
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::digits10( 0), 1, "digits10, 0");
|
|
|
|
tap.expect_eq (cruft::digits10( 1), 1, "digits10, 1");
|
|
|
|
tap.expect_eq (cruft::digits10(10), 2, "digits10, 10");
|
2018-01-01 15:46:29 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::pow (4u,2), 16u, "pow2");
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2017-07-25 17:08:52 +10:00
|
|
|
static const float POS_ZERO = 1.f / numeric_limits<float>::infinity ();
|
|
|
|
|
|
|
|
union {
|
|
|
|
uint32_t neg_zero_bits = 0x80000000;
|
|
|
|
float NEG_ZERO;
|
|
|
|
};
|
2012-05-03 15:59:54 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::sign (-1), -1, "sign(-1)");
|
|
|
|
tap.expect_eq (cruft::sign ( 1), 1, "sign( 1)");
|
|
|
|
tap.expect_eq (cruft::sign (POS_ZERO), 1.f, "sign (POS_ZERO)");
|
|
|
|
tap.expect_eq (cruft::sign (NEG_ZERO), -1.f, "sign (NEG_ZERO)");
|
|
|
|
tap.expect_eq (cruft::sign ( numeric_limits<double>::infinity ()), 1., "sign +inf");
|
|
|
|
tap.expect_eq (cruft::sign (-numeric_limits<double>::infinity ()), -1., "sign -inf");
|
2012-05-03 15:59:54 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::to_degrees (cruft::pi< float>), 180.f, "to_degrees float");
|
|
|
|
tap.expect_eq (cruft::to_degrees (cruft::pi<double>), 180.0, "to_degrees double");
|
|
|
|
tap.expect_eq (cruft::to_radians (180.f), cruft::pi<float>, "to_radians float");
|
|
|
|
tap.expect_eq (cruft::to_radians (180.0), cruft::pi<double>, "to_radians double");
|
2012-05-03 15:59:54 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::log2 (8u), 3u, "log2 +ve");
|
|
|
|
tap.expect_eq (cruft::log2 (1u), 0u, "log2 zero");
|
2014-09-17 16:41:19 +10:00
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
//tap.expect_eq (log2 (9u), 3, "log2up 9");
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect_eq (cruft::log2up (9u), 4u, "log2up 9");
|
|
|
|
tap.expect_eq (cruft::log2up (8u), 3u, "log2up 9");
|
|
|
|
tap.expect_eq (cruft::log2up (1u), 0u, "log2up 9");
|
2014-09-17 16:41:38 +10:00
|
|
|
|
2015-07-13 16:29:01 +10:00
|
|
|
return tap.status ();
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|