diff options
author | kwolekr <kwolekr@minetest.net> | 2015-08-01 01:03:51 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2015-08-01 19:30:08 -0400 |
commit | 42cf5e972d1e27a92048712bc79806e1a088b96c (patch) | |
tree | 06a717e369b1d0ba5a9d5dfb2e628498d724ade1 /src/unittest/test_serialization.cpp | |
parent | bf991bde45b279b40fdfa6c6b6fc6e4dd265c7ee (diff) | |
download | minetest-42cf5e972d1e27a92048712bc79806e1a088b96c.tar.gz minetest-42cf5e972d1e27a92048712bc79806e1a088b96c.tar.bz2 minetest-42cf5e972d1e27a92048712bc79806e1a088b96c.zip |
Improve accuracy and safety of float serialization
Multiplying by a factor of 1/1000.f (rather than dividing by 1000.f) directly
introduces an error of 1 ULP. With this patch, an exact comparison of a
floating point literal with the deserialized F1000 form representing it is now
guaranteed to be successful.
In addition, the maxmium and minimum safely representible floating point
numbers are now well-defined as constants.
Diffstat (limited to 'src/unittest/test_serialization.cpp')
-rw-r--r-- | src/unittest/test_serialization.cpp | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/src/unittest/test_serialization.cpp b/src/unittest/test_serialization.cpp index 7e58dadca..49f348e9c 100644 --- a/src/unittest/test_serialization.cpp +++ b/src/unittest/test_serialization.cpp @@ -296,10 +296,10 @@ void TestSerialization::testStreamRead() UASSERT(readS32(is) == -6); UASSERT(readS64(is) == -43); - UASSERT(fabs(readF1000(is) - 53.534f) < 0.005); - UASSERT(fabs(readF1000(is) - -300000.32f) < 0.05); - UASSERT(fabs(readF1000(is) - -2147483.f) < 0.05); - UASSERT(fabs(readF1000(is) - 2147483.f) < 0.05); + UASSERT(readF1000(is) == 53.534f); + UASSERT(readF1000(is) == -300000.32f); + UASSERT(readF1000(is) == F1000_MIN); + UASSERT(readF1000(is) == F1000_MAX); UASSERT(deSerializeString(is) == "foobar!"); @@ -307,18 +307,11 @@ void TestSerialization::testStreamRead() UASSERT(readV3S16(is) == v3s16(4207, 604, -30)); UASSERT(readV2S32(is) == v2s32(1920, 1080)); UASSERT(readV3S32(is) == v3s32(-400, 6400054, 290549855)); - - v2f vec2 = readV2F1000(is); - UASSERT(fabs(vec2.X - 500.656f) < 0.005); - UASSERT(fabs(vec2.Y - 350.345f) < 0.005); + UASSERT(readV2F1000(is) == v2f(500.656f, 350.345f)); UASSERT(deSerializeWideString(is) == L"\x02~woof~\x5455"); - v3f vec3 = readV3F1000(is); - UASSERT(fabs(vec3.X - 500.f) < 0.005); - UASSERT(fabs(vec3.Y - 10024.2f) < 0.005); - UASSERT(fabs(vec3.Z - -192.54f) < 0.005); - + UASSERT(readV3F1000(is) == v3f(500, 10024.2f, -192.54f)); UASSERT(readARGB8(is) == video::SColor(255, 128, 50, 128)); UASSERT(deSerializeLongString(is) == "some longer string here"); @@ -346,8 +339,8 @@ void TestSerialization::testStreamWrite() writeF1000(os, 53.53467f); writeF1000(os, -300000.32f); - writeF1000(os, -2147483.f); - writeF1000(os, 2147483.f); + writeF1000(os, F1000_MIN); + writeF1000(os, F1000_MAX); os << serializeString("foobar!"); |