aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJude Melton-Houghton <jwmhjwmh@gmail.com>2023-01-12 14:12:31 -0500
committerGitHub <noreply@github.com>2023-01-12 14:12:31 -0500
commit5f2925c59cb3b6fa580e565e2d5a4dad3c400eeb (patch)
treefda5e57028a5f47f2914ad6670c1d765638e8c47
parent956026bb6b20b8f4810404aaa373abb746f73910 (diff)
downloadminetest-5f2925c59cb3b6fa580e565e2d5a4dad3c400eeb.tar.xz
Increase `ftos` precision (#13141)
-rw-r--r--src/unittest/test_settings.cpp14
-rw-r--r--src/util/string.h8
2 files changed, 10 insertions, 12 deletions
diff --git a/src/unittest/test_settings.cpp b/src/unittest/test_settings.cpp
index 6b493c9e4..6f382fabc 100644
--- a/src/unittest/test_settings.cpp
+++ b/src/unittest/test_settings.cpp
@@ -72,7 +72,7 @@ const char *TestSettings::config_text_before =
"some multiline text\n"
" with leading whitespace!\n"
"\"\"\"\n"
- "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n"
+ "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.700012505, 2.40012503\n"
"zoop = true\n"
"[dummy_eof_end_tag]\n";
@@ -100,17 +100,17 @@ const std::string TestSettings::config_text_after =
"\"\"\"\n"
"np_terrain = {\n"
" flags = defaults\n"
- " lacunarity = 2.4\n"
+ " lacunarity = 2.40012503\n"
" octaves = 6\n"
" offset = 3.5\n"
- " persistence = 0.7\n"
+ " persistence = 0.700012505\n"
" scale = 40\n"
" seed = 12341\n"
" spread = (250,250,250)\n"
"}\n"
"zoop = true\n"
"coord2 = (1,2,3.3)\n"
- "floaty_thing_2 = 1.2\n"
+ "floaty_thing_2 = 1.25\n"
"groupy_thing = {\n"
" animals = cute\n"
" num_apples = 4\n"
@@ -160,10 +160,10 @@ void TestSettings::testAllSettings()
UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
// Test the setting of settings too
- s.setFloat("floaty_thing_2", 1.2);
+ s.setFloat("floaty_thing_2", 1.25);
s.setV3F("coord2", v3f(1, 2, 3.3));
- UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2");
- UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
+ UASSERT(s.get("floaty_thing_2").substr(0,4) == "1.25");
+ UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.25) < 0.001);
UASSERT(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
diff --git a/src/util/string.h b/src/util/string.h
index 2f3c2615b..27e2f094d 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include <cstring>
#include <vector>
+#include <limits>
#include <map>
#include <sstream>
#include <iomanip>
@@ -429,14 +430,11 @@ inline std::string itos(s32 i) { return std::to_string(i); }
/// Returns a string representing the decimal value of the 64-bit value \p i.
inline std::string i64tos(s64 i) { return std::to_string(i); }
-// std::to_string uses the '%.6f' conversion, which is inconsistent with
-// std::ostream::operator<<() and impractical too. ftos() uses the
-// more generic and std::ostream::operator<<()-compatible '%G' format.
-/// Returns a string representing the decimal value of the float value \p f.
+/// Returns a string representing the exact decimal value of the float value \p f.
inline std::string ftos(float f)
{
std::ostringstream oss;
- oss << f;
+ oss << std::setprecision(std::numeric_limits<float>::max_digits10) << f;
return oss.str();
}