diff --git a/src/libxrpl/basics/Number.cpp b/src/libxrpl/basics/Number.cpp index e460db9ecc..13b2deca92 100644 --- a/src/libxrpl/basics/Number.cpp +++ b/src/libxrpl/basics/Number.cpp @@ -1,4 +1,5 @@ #include +// #include #include @@ -26,7 +27,7 @@ namespace std { template <> struct make_unsigned { - using type = typename uint128_t; + using type = uint128_t; }; } // namespace std @@ -585,27 +586,33 @@ to_string(Number const& amount) return "0"; auto const exponent = amount.exponent(); - auto mantissa = amount.mantissa(); + + bool const negative = amount.mantissa() < 0; + + auto const mantissa = [&]() { + auto mantissa = amount.mantissa(); + if (negative) + { + mantissa = -mantissa; + } + XRPL_ASSERT( + mantissa < std::numeric_limits::max(), + "ripple::to_string(Number) : mantissa fits in uin64_t"); + return static_cast(mantissa); + }(); // Use scientific notation for exponents that are too small or too large auto const rangeLog = Number::mantissaLog(); if (((exponent != 0) && ((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10))))) { - std::string ret = to_string(mantissa); + std::string ret = negative ? "-" : ""; + ret.append(std::to_string(mantissa)); ret.append(1, 'e'); ret.append(std::to_string(exponent)); return ret; } - bool negative = false; - - if (mantissa < 0) - { - mantissa = -mantissa; - negative = true; - } - // TODO: These numbers are probably wrong for largeRange XRPL_ASSERT( exponent + 43 > 0, "ripple::to_string(Number) : minimum exponent"); @@ -613,7 +620,7 @@ to_string(Number const& amount) ptrdiff_t const pad_prefix = Number::mantissaLog() + 12; ptrdiff_t const pad_suffix = Number::mantissaLog() + 8; - std::string const raw_value(to_string(mantissa)); + std::string const raw_value(std::to_string(mantissa)); std::string val; val.reserve(raw_value.length() + pad_prefix + pad_suffix);