Skip to content

Commit

Permalink
Increased snbt float precision
Browse files Browse the repository at this point in the history
Some values did not decode back to themselves. Hopefully this will fix that.
  • Loading branch information
gentlegiantJGC committed Feb 20, 2024
1 parent 06cf8f7 commit ec2085b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/amulet_nbt/_nbt_encoding/_string/_cpp/write_snbt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <locale>
#include <sstream>
#include <iomanip>
#include <stdexcept>

#include "../../../_string_encoding/_cpp/utf8.hpp"
#include "../../../_tag/_cpp/nbt.hpp"
Expand Down Expand Up @@ -103,13 +104,13 @@ void write_long_snbt(std::string& snbt, const CLongTag& tag){
template<typename T>
inline std::string encode_float(const T& num){
std::ostringstream oss;
oss << std::setprecision(std::numeric_limits<T>::digits10 + 1) << std::noshowpoint << num;
oss << std::setprecision(std::numeric_limits<T>::max_digits10) << std::noshowpoint << num;
return oss.str();
}

void write_float_snbt(std::string& snbt, const CFloatTag& tag){
if (std::isfinite(tag)){
snbt.append(encode_float(tag));
snbt.append(encode_float<CFloatTag>(tag));
snbt.push_back('f');
} else if (tag == std::numeric_limits<CFloatTag>::infinity()){
snbt.append("Infinityf");
Expand All @@ -122,7 +123,7 @@ void write_float_snbt(std::string& snbt, const CFloatTag& tag){

void write_double_snbt(std::string& snbt, const CDoubleTag& tag){
if (std::isfinite(tag)){
snbt.append(encode_float(tag));
snbt.append(encode_float<CDoubleTag>(tag));
snbt.push_back('d');
} else if (tag == std::numeric_limits<CDoubleTag>::infinity()){
snbt.append("Infinityd");
Expand Down Expand Up @@ -298,7 +299,7 @@ std::vector<std::pair<std::string, TagNode>> sort_compound(const CCompoundTagPtr
std::locale locale;
try {
locale = std::locale("en_US.UTF-8");
} catch (const std::runtime_error& error) {
} catch (const std::runtime_error&) {
locale = std::locale("");
}
std::sort(keys.begin(), keys.end(), [&locale](
Expand Down
32 changes: 16 additions & 16 deletions tests/test_amulet_nbt/test_tag/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@

ToSNBTData: list[tuple[float, str, str]] = [
(float("-inf"), "-Infinityf", "-Infinityd"),
(-9999999999999999, "-1e+16f", "-1e+16d"),
(-99999999, "-1e+08f", "-99999999d"),
(-9999999999999999, "-1.00000003e+16f", "-10000000000000000d"),
(-99999999, "-100000000f", "-99999999d"),
(-9999, "-9999f", "-9999d"),
(-99, "-99f", "-99d"),
(-9, "-9f", "-9d"),
(-1, "-1f", "-1d"),
(-0.1, "-0.1f", "-0.1d"),
(-0.01, "-0.01f", "-0.01d"),
(-0.001, "-0.001f", "-0.001d"),
(-0.00001, "-1e-05f", "-1e-05d"),
(-0.000000001, "-1e-09f", "-1e-09d"),
(-0.00000000000000001, "-1e-17f", "-1e-17d"),
(-0.1, "-0.100000001f", "-0.10000000000000001d"),
(-0.01, "-0.00999999978f", "-0.01d"),
(-0.001, "-0.00100000005f", "-0.001d"),
(-0.00001, "-9.99999975e-06f", "-1.0000000000000001e-05d"),
(-0.000000001, "-9.99999972e-10f", "-1.0000000000000001e-09d"),
(-0.00000000000000001, "-9.99999984e-18f", "-1.0000000000000001e-17d"),
(float("-0"), "-0f", "-0d"),
(float("+0"), "0f", "0d"),
(0.00000000000000001, "1e-17f", "1e-17d"),
(0.000000001, "1e-09f", "1e-09d"),
(0.00001, "1e-05f", "1e-05d"),
(0.001, "0.001f", "0.001d"),
(0.01, "0.01f", "0.01d"),
(0.1, "0.1f", "0.1d"),
(0.00000000000000001, "9.99999984e-18f", "1.0000000000000001e-17d"),
(0.000000001, "9.99999972e-10f", "1.0000000000000001e-09d"),
(0.00001, "9.99999975e-06f", "1.0000000000000001e-05d"),
(0.001, "0.00100000005f", "0.001d"),
(0.01, "0.00999999978f", "0.01d"),
(0.1, "0.100000001f", "0.10000000000000001d"),
(1, "1f", "1d"),
(9, "9f", "9d"),
(99, "99f", "99d"),
(9999, "9999f", "9999d"),
(99999999, "1e+08f", "99999999d"),
(9999999999999999, "1e+16f", "1e+16d"),
(99999999, "100000000f", "99999999d"),
(9999999999999999, "1.00000003e+16f", "10000000000000000d"),
(float("inf"), "Infinityf", "Infinityd"),
(
struct.unpack(">d", b"\x7f\xf0\x00\x00\x00\x00\x00\x01")[0],
Expand Down

0 comments on commit ec2085b

Please sign in to comment.