Skip to content

Commit

Permalink
Adds more tests for fixed16 to validate that division has appropriate…
Browse files Browse the repository at this point in the history
… precision. (#595)

This does not change the implementation. The existing implementation already had a givision algorithm with good precision. The additional tests will ensure that this property will hold up in the future.
  • Loading branch information
balazsracz authored Dec 27, 2021
1 parent 83b7624 commit 31d0f15
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/utils/Fixed16.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,46 @@ TEST(Fixed16Test, Division)
EXPECT_EQ(0, multiplier.trunc());
}

TEST(Fixed16Test, ShiftByDivision)
{
Fixed16 v1(1);
v1 /= 4;
EXPECT_EQ(0, v1.trunc());
EXPECT_EQ(0x4000, v1.frac());

v1 = 0x5650;
v1 /= 16;
EXPECT_EQ(0x565, v1.trunc());
EXPECT_EQ(0, v1.frac());

v1 = 0x569F;
v1 /= 256;
EXPECT_EQ(0x56, v1.trunc());
EXPECT_EQ(0x9F00, v1.frac());

// Negative bit is also correctly handled.
v1 = 0x569F;
v1 /= -256;
EXPECT_EQ(-0x56, v1.trunc());
EXPECT_EQ(0x9F00, v1.frac());
}

TEST(Fixed16Test, DivisionPrecision)
{
Fixed16 v1(31000); // close to largest representable integer
Fixed16 v2(Fixed16::FROM_DOUBLE, 1.01754);
EXPECT_EQ(1150, v2.frac());
v1 /= v2;
// - True result is 30465.6328
// - Due to rounding while assigning v2, we are actually dividing
// by 1.0175476
// - Precise division result is then 30465.40503
EXPECT_EQ(30465, v1.trunc());
EXPECT_EQ((unsigned)(0.40503 * 65536), v1.frac());
EXPECT_THAT(v1.to_float(), FloatNear(30465.40503, 1e-4));
EXPECT_THAT(v1.to_float(), FloatNear(31000.0 / 1.0175476, 1e-4));
}

TEST(Fixed16Test, Sign)
{
Fixed16 v1(13);
Expand Down

0 comments on commit 31d0f15

Please sign in to comment.