mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-18 05:58:19 +00:00
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:
parent
fa8640bd35
commit
859727c1d3
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
14
src/test/math.cpp
Normal file
14
src/test/math.cpp
Normal 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)));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue