diff --git a/include/rfl/Generic.hpp b/include/rfl/Generic.hpp index eb4e90c7..32a23576 100644 --- a/include/rfl/Generic.hpp +++ b/include/rfl/Generic.hpp @@ -126,13 +126,23 @@ class RFL_API Generic { } /// Casts the underlying value to a double or returns an rfl::Error, if the - /// underlying value is not a double. + /// underlying value is not a number or the conversion would result in loss of + /// precision. Result to_double() const noexcept { return std::visit( [](auto _v) -> Result { using V = std::remove_cvref_t; if constexpr (std::is_same_v) { return _v; + } else if constexpr (std::is_same_v) { + auto _d = static_cast(_v); + if (static_cast(_d) == _v) { + return _d; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to a " + "double without loss of precision."); + } } else { return error( "rfl::Generic: Could not cast the underlying value to a "