Skip to content

Commit

Permalink
Fix inaccurate envelope point value rounding
Browse files Browse the repository at this point in the history
Round to nearest integer instead of truncating in `f2fx` to ensure correct round-trip with `fx2f`.

Add test to ensure correct round-trip with maximum `0.0005f` absolute error.
  • Loading branch information
Robyt3 committed Nov 10, 2023
1 parent fa8640b commit 5450fba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
jsonwriter.cpp
linereader.cpp
mapbugs.cpp
math.cpp
name_ban.cpp
net.cpp
netaddr.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/base/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ constexpr int fxpscale = 1 << 10;
// float to fixed
constexpr inline int f2fx(float v)
{
return (int)(v * fxpscale);
return round_to_int(v * fxpscale);
}
constexpr inline float fx2f(int v)
{
Expand Down
13 changes: 13 additions & 0 deletions src/test/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "test.h"
#include <gtest/gtest.h>

#include <base/math.h>

TEST(Math, FixedPointRoundtrip)
{
for(int i = 0; i < 100000; ++i)
{
const float Number = i / 1000.0f;
EXPECT_NEAR(Number, fx2f(f2fx(Number)), 0.0005f);
}
}

0 comments on commit 5450fba

Please sign in to comment.