Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Custom abs(), that correctly handles most-negative values on 2's complement systems.
- // Requires T to be either unsigned, or convertible to unsigned.
- #include <type_traits>
- template<typename T, typename R = std::make_unsigned_t<T>>
- R twos_compl_abs(T val) {
- if (val >= 0) { return val; }
- else { return static_cast<R>(val) * -1; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement