Increase ftos precision (#13141)

This commit is contained in:
Jude Melton-Houghton 2023-01-12 14:12:31 -05:00 committed by GitHub
parent 956026bb6b
commit 5f2925c59c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 12 deletions

@ -72,7 +72,7 @@ const char *TestSettings::config_text_before =
"some multiline text\n" "some multiline text\n"
" with leading whitespace!\n" " with leading whitespace!\n"
"\"\"\"\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" "zoop = true\n"
"[dummy_eof_end_tag]\n"; "[dummy_eof_end_tag]\n";
@ -100,17 +100,17 @@ const std::string TestSettings::config_text_after =
"\"\"\"\n" "\"\"\"\n"
"np_terrain = {\n" "np_terrain = {\n"
" flags = defaults\n" " flags = defaults\n"
" lacunarity = 2.4\n" " lacunarity = 2.40012503\n"
" octaves = 6\n" " octaves = 6\n"
" offset = 3.5\n" " offset = 3.5\n"
" persistence = 0.7\n" " persistence = 0.700012505\n"
" scale = 40\n" " scale = 40\n"
" seed = 12341\n" " seed = 12341\n"
" spread = (250,250,250)\n" " spread = (250,250,250)\n"
"}\n" "}\n"
"zoop = true\n" "zoop = true\n"
"coord2 = (1,2,3.3)\n" "coord2 = (1,2,3.3)\n"
"floaty_thing_2 = 1.2\n" "floaty_thing_2 = 1.25\n"
"groupy_thing = {\n" "groupy_thing = {\n"
" animals = cute\n" " animals = cute\n"
" num_apples = 4\n" " num_apples = 4\n"
@ -160,10 +160,10 @@ void TestSettings::testAllSettings()
UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001); UASSERT(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
// Test the setting of settings too // 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)); s.setV3F("coord2", v3f(1, 2, 3.3));
UASSERT(s.get("floaty_thing_2").substr(0,3) == "1.2"); UASSERT(s.get("floaty_thing_2").substr(0,4) == "1.25");
UASSERT(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001); 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").X - 1.0) < 0.001);
UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001); UASSERT(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001); UASSERT(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);

@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <limits>
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <iomanip> #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. /// 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); } inline std::string i64tos(s64 i) { return std::to_string(i); }
// std::to_string uses the '%.6f' conversion, which is inconsistent with /// Returns a string representing the exact decimal value of the float value \p f.
// 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.
inline std::string ftos(float f) inline std::string ftos(float f)
{ {
std::ostringstream oss; std::ostringstream oss;
oss << f; oss << std::setprecision(std::numeric_limits<float>::max_digits10) << f;
return oss.str(); return oss.str();
} }