From 8253c06d84f35f211bc633df616a32008373bdbc Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sun, 26 Nov 2017 11:35:25 +0100 Subject: Improved to_uint32 Instead of doing two comparisons (one with UINT32_MAX and one with 0), we count the number of operands in the number to determine if it would fit in a 32-bits-wide integer. Signed-off-by: Olivier Gayot --- number.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/number.cpp b/number.cpp index d39ed36..074c4dc 100644 --- a/number.cpp +++ b/number.cpp @@ -63,11 +63,13 @@ number::to_dec_string() const std::uint32_t number::to_uint32() const { - if (*this > UINT32_MAX) { + auto size = _operands.size(); + + if (size > 1) { throw std::out_of_range("> UINT32_MAX"); } - return (*this == 0) ? 0 : _operands.front(); + return (size == 0) ? 0 : _operands.front(); } std::uint64_t -- cgit v1.2.3