1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-31 18:31:04 +00:00

MetaDataRef: Make set_float preserve numbers exactly (#16090)

This commit is contained in:
Lars Müller 2025-05-02 21:27:00 +02:00 committed by GitHub
parent 6f3735281f
commit d96f5e1c76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 92 additions and 12 deletions

View file

@ -1065,3 +1065,30 @@ std::optional<v3f> str_to_v3f(std::string_view str)
return value;
}
std::string my_double_to_string(double number)
{
if (std::isfinite(number)) {
char buf[64];
snprintf(buf, sizeof(buf), "%.17g", number);
return buf;
}
if (number < 0)
return "-inf";
if (number > 0)
return "inf";
return "nan";
}
std::optional<double> my_string_to_double(const std::string &s)
{
if (s.empty())
return std::nullopt;
char *end = nullptr;
errno = 0;
// Note: this also supports hexadecimal notation like "0x1.0p+1"
double number = std::strtod(s.data(), &end);
if (end != &*s.end())
return std::nullopt;
return number;
}