2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/float.hpp>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
|
|
|
#include <cruft/util/types.hpp>
|
2011-06-21 20:16:39 +10:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
2016-03-11 13:28:56 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_double (cruft::TAP::logger &tap)
|
2016-01-19 18:31:49 +11:00
|
|
|
{
|
2011-05-23 17:18:52 +10:00
|
|
|
struct sized_test {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::ieee_double::uint_t bits;
|
2016-10-11 23:47:57 +11:00
|
|
|
double floating;
|
2011-05-23 17:18:52 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
sized_test tests[] = {
|
|
|
|
{ 0x3ff0000000000000, 1.0 },
|
2016-03-11 13:28:56 +11:00
|
|
|
{ 0x3ff0000000000001, 1.0 + std::numeric_limits<double>::epsilon () },
|
|
|
|
{ 0x3ff0000000000002, 1.0 + std::numeric_limits<double>::epsilon () * 2},
|
2011-05-23 17:18:52 +10:00
|
|
|
{ 0x4000000000000000, 2.0 },
|
|
|
|
{ 0xc000000000000000, -2.0 },
|
2016-03-11 13:28:56 +11:00
|
|
|
{ 0x0000000000000001, std::numeric_limits<double>::denorm_min () },
|
|
|
|
{ 0x0010000000000000, std::numeric_limits<double>::min () }, // min positive normal
|
|
|
|
{ 0x7fefffffffffffff, std::numeric_limits<double>::max () }, // max
|
2011-05-23 17:18:52 +10:00
|
|
|
{ 0x0000000000000000, 0.0 },
|
|
|
|
{ 0x8000000000000000, -0.0 },
|
2016-03-11 13:28:56 +11:00
|
|
|
{ 0x7ff0000000000000, std::numeric_limits<double>::infinity() },
|
|
|
|
{ 0xfff0000000000000, -std::numeric_limits<double>::infinity() },
|
2011-05-23 17:18:52 +10:00
|
|
|
{ 0x3fd5555555555555, 1.0 / 3.0 }
|
|
|
|
};
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
bool success = true;
|
|
|
|
|
2016-11-17 18:06:39 +11:00
|
|
|
for (unsigned int i = 0; i < std::size (tests); ++i) {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::ieee_double val;
|
2011-05-23 17:18:52 +10:00
|
|
|
val.set_bits (tests[i].bits);
|
2016-01-19 18:31:49 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
success = success && cruft::equal (val, tests[i].floating);
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
2016-01-19 18:31:49 +11:00
|
|
|
|
|
|
|
tap.expect (success, "double precision bitwise equality");
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-11 13:28:56 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-04-13 18:06:08 +10:00
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_single (cruft::TAP::logger &tap)
|
2016-01-19 18:31:49 +11:00
|
|
|
{
|
2011-05-23 17:18:52 +10:00
|
|
|
struct sized_test {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::ieee_single::uint_t bits;
|
2016-10-11 23:47:57 +11:00
|
|
|
float floating;
|
2011-05-23 17:18:52 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
sized_test tests[] = {
|
|
|
|
{ 0x3f800000, 1.0f },
|
|
|
|
{ 0xc0000000, -2.0f },
|
2016-03-11 13:28:56 +11:00
|
|
|
{ 0x7f7fffff, std::numeric_limits<float>::max () },
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
{ 0x00000000, 0.0f },
|
|
|
|
{ 0x80000000, -0.0f },
|
|
|
|
|
2016-03-11 13:28:56 +11:00
|
|
|
{ 0x7f800000, std::numeric_limits<float>::infinity () },
|
|
|
|
{ 0xff800000, -std::numeric_limits<float>::infinity () },
|
2015-04-13 18:06:08 +10:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
{ 0x3eaaaaab, 1.0f / 3.0f }
|
|
|
|
};
|
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
bool success = true;
|
|
|
|
|
2016-11-17 18:06:39 +11:00
|
|
|
for (unsigned int i = 0; i < std::size (tests); ++i) {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::ieee_single val;
|
2011-05-23 17:18:52 +10:00
|
|
|
val.set_bits (tests[i].bits);
|
2016-01-19 18:31:49 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
success = success && cruft::equal (val, tests[i].floating);
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
2016-01-19 18:31:49 +11:00
|
|
|
|
|
|
|
tap.expect (success, "single precision bitwise equality");
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-11 13:28:56 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
int
|
|
|
|
main (int, char **) {
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2016-01-19 18:31:49 +11:00
|
|
|
|
|
|
|
test_single (tap);
|
|
|
|
test_double (tap);
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
tap.expect (cruft::almost_equal (1.f, 1), "trivial float/int almost-equality");
|
|
|
|
tap.expect (cruft::almost_equal (1, 1.f), "trivial int/float almost-equality");
|
2018-02-26 13:37:08 +11:00
|
|
|
|
2016-01-19 18:31:49 +11:00
|
|
|
return tap.status ();
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|