mirror of
https://github.com/luanti-org/luanti.git
synced 2025-11-02 00:05:26 +01:00
MetaDataRef: Make set_float preserve numbers exactly (#16090)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -465,6 +465,10 @@ inline std::string ftos(float f)
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
/// @brief Converts double to string. Handles high precision and inf/nan.
|
||||
std::string my_double_to_string(double number);
|
||||
/// @brief Converts string to double. Handles high precision and inf/nan.
|
||||
std::optional<double> my_string_to_double(const std::string &s);
|
||||
|
||||
/**
|
||||
* Replace all occurrences of \p pattern in \p str with \p replacement.
|
||||
|
||||
Reference in New Issue
Block a user