Fix inaccurate envelope point value rounding

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.
This commit is contained in:
Robert Müller 2023-11-10 17:11:22 +01:00
parent fa8640bd35
commit 859727c1d3
3 changed files with 16 additions and 1 deletions

View file

@ -2744,6 +2744,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
jsonwriter.cpp jsonwriter.cpp
linereader.cpp linereader.cpp
mapbugs.cpp mapbugs.cpp
math.cpp
name_ban.cpp name_ban.cpp
net.cpp net.cpp
netaddr.cpp netaddr.cpp

View file

@ -66,7 +66,7 @@ constexpr int fxpscale = 1 << 10;
// float to fixed // float to fixed
constexpr inline int f2fx(float v) constexpr inline int f2fx(float v)
{ {
return (int)(v * fxpscale); return round_to_int(v * fxpscale);
} }
constexpr inline float fx2f(int v) constexpr inline float fx2f(int v)
{ {

14
src/test/math.cpp Normal file
View file

@ -0,0 +1,14 @@
#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);
EXPECT_EQ(i, f2fx(fx2f(i)));
}
}