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:
parent
6f3735281f
commit
d96f5e1c76
6 changed files with 92 additions and 12 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue