diff options
author | Olivier Gayot <og@satcom1.com> | 2017-11-26 11:35:25 +0100 |
---|---|---|
committer | Olivier Gayot <og@satcom1.com> | 2017-11-26 11:37:09 +0100 |
commit | 8253c06d84f35f211bc633df616a32008373bdbc (patch) | |
tree | 1dcab4313cdf4c25b6c987b7b9ca92cfbddeb44e /number.cpp | |
parent | 07f6748c9ae255227903bde4fe359bc31c943494 (diff) |
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 <og@satcom1.com>
Diffstat (limited to 'number.cpp')
-rw-r--r-- | number.cpp | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -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 |