jaulib: Integer types and arithmetic (original) (raw)
Integral integer types and arithmetic. More...
| Classes | |
|---|---|
| class | jau::mp::BigInt |
| Arbitrary precision integer type. More... | |
| struct | jau::float_bytes< bytesize > |
| struct | jau::float_bytes< sizeof(double)> |
| struct | jau::float_bytes< sizeof(float)> |
| struct | jau::float_bytes< sizeof(long double)> |
| class | jau::int_ctti |
| struct | jau::sint_bytes< bytesize > |
| struct | jau::sint_bytes< 1 > |
| struct | jau::sint_bytes< 2 > |
| struct | jau::sint_bytes< 4 > |
| struct | jau::sint_bytes< 8 > |
| struct | jau::uint128dp_t |
| A 128-bit packed uint8_t data array. More... | |
| struct | jau::uint192dp_t |
| A 196-bit packed uint8_t data array. More... | |
| struct | jau::uint256dp_t |
| A 256-bit packed uint8_t data array. More... | |
| struct | jau::uint_bytes< bytesize > |
| struct | jau::uint_bytes< 1 > |
| struct | jau::uint_bytes< 2 > |
| struct | jau::uint_bytes< 4 > |
| struct | jau::uint_bytes< 8 > |
| Typedefs | |
|---|---|
| template | |
| using | jau::float_bytes_t = typename float_bytes::type |
| Alias template for float_bytes. | |
| typedef uint_bytes_t< sizeof(unsigned long int)> | jau::nsize_t |
| Natural 'size_t' alternative using uint_t with xx = sizeof(unsigned long int)*8 as its natural sized type, i.e. | |
| template | |
| using | jau::sint_bytes_t = typename sint_bytes::type |
| Alias template for sint_bytes. | |
| typedef sint_bytes_t< sizeof(long int)> | jau::snsize_t |
| Natural 'ssize_t' alternative using int_t with xx = sizeof(long int)*8 as its natural sized type, i.e. | |
| template | |
| using | jau::uint_bytes_t = typename uint_bytes::type |
| Alias template for uint_bytes. | |
| Functions | |
|---|---|
| mp::BigInt | jau::abs (const mp::BigInt &x) noexcept |
| template<jau::req::signed_arithmetic T> | |
| constexpr T | jau::abs (const T x) noexcept |
| Returns the absolute value of an arithmetic number (w/ branching) in O(1) | |
| template<std::integral T> | |
| constexpr bool | jau::add_overflow (const T a, const T b, T &res) noexcept |
| Integer overflow aware addition returning true if overflow occurred, otherwise false having the result stored in res. | |
| template<jau::req::unsigned_integral T> | |
| constexpr T | jau::bit_ceil (const T n) noexcept |
| If the given n is not is_power_of_2() return next_power_of_2(), otherwise return n unchanged. | |
| template<jau::req::unsigned_integral T> | |
| constexpr size_t | jau::bit_count (T n) noexcept |
| Returns the number of set bits within given unsigned integral. | |
| const mp::BigInt & | jau::clamp (const mp::BigInt &x, const mp::BigInt &min_val, const mp::BigInt &max_val) noexcept |
| template<jau::req::arithmetic T> | |
| constexpr T | jau::clamp (const T x, const T min_val, const T max_val) noexcept |
| Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1). | |
| mp::BigInt & | jau::clamp (mp::BigInt &x, mp::BigInt &min_val, mp::BigInt &max_val) noexcept |
| template<jau::req::arithmetic R, jau::req::arithmetic T> | |
| constexpr R | jau::clampCast (const T x, const T min_val, const T max_val) noexcept |
| Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1), cast to type R. | |
| void | jau::clear_bit_uint32 (const uint8_t nr, uint32_t &mask) |
| void | jau::clear_bit_uint64 (const uint8_t nr, uint64_t &mask) |
| template<jau::req::unsigned_integral T> | |
| constexpr uint32_t | jau::digits (const T x, const uint32_t radix) noexcept |
| Returns the number of digits of the given unsigned integral value number and the given radix. | |
| template<jau::req::signed_integral T> | |
| constexpr uint32_t | jau::digits10 (const T x) noexcept |
| Returns the number of decimal digits of the given signed integral value number using std::log10() including a negative sign of value x as a digit. | |
| template<std::integral T> | |
| constexpr uint32_t | jau::digits10 (const T x, const bool sign_is_digit) noexcept |
| Returns the number of decimal digits of the given integral value number using std::log10(). | |
| template<std::integral T> | |
| constexpr uint32_t | jau::digits10 (const T x, const int x_sign, const bool sign_is_digit=true) noexcept |
| Returns the number of decimal digits of the given integral value number using std::log10(). | |
| template<std::integral T> | |
| constexpr bool | jau::equals (const T &a, const T &b) noexcept |
| Returns true if both values are equal. | |
| mp::BigInt | jau::gcd (const mp::BigInt &a, const mp::BigInt &b) noexcept |
| template<jau::req::signed_integral T> | |
| constexpr T | jau::gcd (T a, T b) noexcept |
| Returns the greatest common divisor (GCD) of the two given integer values following Euclid's algorithm from Euclid's Elements ~300 BC, using the absolute positive value of given integers. | |
| consteval_cxx20 bool | jau::has_builtin_add_overflow () noexcept |
| Query whether __builtin_add_overflow is available via __has_builtin(__builtin_add_overflow). | |
| consteval_cxx20 bool | jau::has_builtin_mul_overflow () noexcept |
| Query whether __builtin_mul_overflow is available via __has_builtin(__builtin_mul_overflow). | |
| consteval_cxx20 bool | jau::has_builtin_sub_overflow () noexcept |
| Query whether __builtin_sub_overflow is available via __has_builtin(__builtin_sub_overflow). | |
| template<jau::req::unsigned_integral T> | |
| constexpr nsize_t | jau::high_bit (T x) |
| Return the index of the highest set bit w/ branching (loop) in O(n/2). | |
| template<jau::req::arithmetic T> | |
| bool | jau::in_range (const T &a, const T &b, const T &range) |
| base_math: arithmetic types, i.e. | |
| template<jau::req::signed_arithmetic T> | |
| constexpr T | jau::invert_sign (const T x) noexcept |
| Safely inverts the sign of an arithmetic number w/ branching in O(1) | |
| template requires jau::req::signed_integral | |
| constexpr bool | jau::is_positive (const T a) noexcept |
| Returns true of the given integral is positive, i.e. | |
| template<jau::req::unsigned_integral T> | |
| constexpr bool | jau::is_power_of_2 (const T x) noexcept |
| Power of 2 test in O(1), i.e. | |
| template<std::integral T> | |
| constexpr bool | jau::is_zero (const T &a) noexcept |
| base_math: arithmetic types, i.e. | |
| template<std::integral T> | |
| constexpr bool | jau::lcm_overflow (const T a, const T b, T &result) noexcept |
| Integer overflow aware calculation of least common multiple (LCM) following Euclid's algorithm from Euclid's Elements ~300 BC. | |
| constexpr size_t | jau::log2_byteshift (const size_t bytesize) noexcept |
| Returns log2(bytesize*8), e.g. | |
| const mp::BigInt & | jau::max (const mp::BigInt &x, const mp::BigInt &y) noexcept |
| template<jau::req::arithmetic T> | |
| constexpr T | jau::max (const T x, const T y) noexcept |
| Returns the maximum of two integrals (w/ branching) in O(1) | |
| const mp::BigInt & | jau::max (mp::BigInt &&, mp::BigInt &&) noexcept=delete |
| mp::BigInt & | jau::max (mp::BigInt &x, mp::BigInt &y) noexcept |
| uint128dp_t | jau::merge_uint128 (uint16_t const uuid16, uint128dp_t const &base_uuid, nsize_t const uuid16_le_octet_index) |
| Merge the given 'uuid16' into a 'base_uuid' copy at the given little endian 'uuid16_le_octet_index' position. | |
| uint128dp_t | jau::merge_uint128 (uint32_t const uuid32, uint128dp_t const &base_uuid, nsize_t const uuid32_le_octet_index) |
| Merge the given 'uuid32' into a 'base_uuid' copy at the given little endian 'uuid32_le_octet_index' position. | |
| const mp::BigInt & | jau::min (const mp::BigInt &x, const mp::BigInt &y) noexcept |
| template<jau::req::arithmetic T> | |
| constexpr T | jau::min (const T x, const T y) noexcept |
| Returns the minimum of two integrals (w/ branching) in O(1) | |
| const mp::BigInt & | jau::min (mp::BigInt &&, mp::BigInt &&) noexcept=delete |
| mp::BigInt & | jau::min (mp::BigInt &x, mp::BigInt &y) noexcept |
| template<std::integral T> | |
| constexpr bool | jau::mul_overflow (const T a, const T b, T &res) noexcept |
| Integer overflow aware multiplication returning true if overflow occurred, otherwise false having the result stored in res. | |
| mp::BigInt | jau::pow (mp::BigInt b, const mp::BigInt &e) |
| template<jau::req::unsigned_integral T, jau::req::unsigned_integral U> | |
| constexpr T | jau::round_down (T n, U align_to) |
| Round down w/ branching in O(1) | |
| template<jau::req::unsigned_integral T, jau::req::unsigned_integral U> | |
| constexpr T | jau::round_up (const T n, const U align_to) |
| Round up w/ branching in O(1) | |
| void | jau::set_bit_uint32 (const uint8_t nr, uint32_t &mask) |
| void | jau::set_bit_uint64 (const uint8_t nr, uint64_t &mask) |
| template<jau::req::signed_arithmetic T> | |
| constexpr int | jau::sign (const T x) noexcept |
| Returns the value of the sign function (w/o branching ?) in O(1). | |
| template<std::integral T> | |
| constexpr bool | jau::sub_overflow (const T a, const T b, T &res) noexcept |
| Integer overflow aware subtraction returning true if overflow occurred, otherwise false having the result stored in res. | |
| uint32_t | jau::test_bit_uint32 (const uint8_t nr, const uint32_t mask) |
| uint64_t | jau::test_bit_uint64 (const uint8_t nr, const uint64_t mask) |
| template<jau::req::signed_arithmetic T> | |
| constexpr std::make_unsigned_t< T > | jau::unsigned_value (const T x) noexcept |
| Returns the unsigned typed absolute value of an arithmetic number (w/ branching) in O(1) | |
Integral integer types and arithmetic.
Further support is coming from Byte Utilities and meta-group Math Support
◆ uint_bytes_t
◆ sint_bytes_t
◆ float_bytes_t
◆ nsize_t
typedef uint_bytes_t<sizeof(unsigned long int)> jau::nsize_t
Natural 'size_t' alternative using uint<XX>_t with xx = sizeof(unsigned long int)*8 as its natural sized type, i.e.
either uint32_t (ILP32, LLP64), uint64_t (LP64, ILP64, SILP64) or uint128_t (future).
This is a compromise to indicate intend and to avoid using a multiple sized size_t footprint where not desired.
Note: size_t on LLP64 model is 64-bit, while unsigned long int and hence jau::nsize_t remains 32-bit.
See also
Examples
Definition at line 89 of file int_types.hpp.
◆ snsize_t
typedef sint_bytes_t<sizeof(long int)> jau::snsize_t
Natural 'ssize_t' alternative using int<XX>_t with xx = sizeof(long int)*8 as its natural sized type, i.e.
either int32_t (ILP32, LLP64), int64_t (LP64, ILP64, SILP64) or int128_t (future).
This is a compromise to indicate intend and to avoid using a multiple sized size_t footprint where not desired.
Note: ssize_t on LLP64 model is 64-bit, while long int and hence jau::snsize_t remains 32-bit.
See also
Definition at line 101 of file int_types.hpp.
◆ in_range()
| bool jau::in_range | ( | const T & | a, |
|---|---|---|---|
| const T & | b, | ||
| const T & | range ) |
base_math: arithmetic types, i.e.
integral + floating point types int_math: integral types float_math: floating point types Returns true, if both integer point values differ less than the given range.
Template Parameters
Parameters
| a | value to compare |
|---|---|
| b | value to compare |
| range | the maximum difference both values may differ |
Definition at line 63 of file base_math.hpp.
◆ is_positive()
| bool jau::is_positive ( const T a) | constexprnoexcept |
|---|
Returns true of the given integral is positive, i.e.
>= 0.
Definition at line 70 of file base_math.hpp.
◆ sign()
| int jau::sign ( const T x) | constexprnoexcept |
|---|
Returns the value of the sign function (w/o branching ?) in O(1).
-1 for x < 0 0 for x = 0 1 for x > 0
Implementation is type safe.
Branching may occur due to relational operator.
Template Parameters
| T | an arithmetic number type |
|---|
Parameters
Returns
function result
Definition at line 102 of file base_math.hpp.
◆ invert_sign()
| T jau::invert_sign ( const T x) | constexprnoexcept |
|---|
Safely inverts the sign of an arithmetic number w/ branching in O(1)
Implementation takes special care to have T_MIN, i.e. std::numeric_limits::min(), converted to T_MAX, i.e. std::numeric_limits::max().
This is necessary since T_MAX < | -T_MIN | and the result would not fit in the return type T otherwise.
Hence for the extreme minimum case:
jau::invert_sign(INT32_MIN) = | INT32_MIN | - 1 = INT32_MAX
Otherwise with x < 0:
jau::invert_sign(x) = | x | = -x
and x >= 0:
jau::invert_sign(x) = -x
Template Parameters
| T | an unsigned arithmetic number type |
|---|
Parameters
Returns
function result
Definition at line 140 of file base_math.hpp.
◆ abs() [1/2]
| T jau::abs ( const T x) | constexprnoexcept |
|---|
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
- signed uses jau::invert_sign() to have a safe absolute value conversion
- unsigned just returns the value
- 2-complement branch-less is not used due to lack of INT_MIN -> INT_MAX conversion, bithacks Integer-Abs
This implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
Template Parameters
| T | an arithmetic number type |
|---|
Parameters
Returns
function result
Definition at line 165 of file base_math.hpp.
◆ unsigned_value()
| std::make_unsigned_t< T > jau::unsigned_value ( const T x) | constexprnoexcept |
|---|
Returns the unsigned typed absolute value of an arithmetic number (w/ branching) in O(1)
Implementation handles 2-complement case INT_MIN -> -INT_MIN, i.e. INT_MAX+1 as unsigned
Template Parameters
| T | an arithmetic number type |
|---|
Parameters
Returns
function result
Definition at line 186 of file base_math.hpp.
◆ min() [1/4]
| T jau::min ( const T x, const T y ) | constexprnoexcept |
|---|
Returns the minimum of two integrals (w/ branching) in O(1)
Template Parameters
| T | an arithmetic number type |
|---|
Parameters
| x | one number |
|---|---|
| x | the other number |
Definition at line 213 of file base_math.hpp.
◆ max() [1/4]
| T jau::max ( const T x, const T y ) | constexprnoexcept |
|---|
Returns the maximum of two integrals (w/ branching) in O(1)
Template Parameters
| T | an arithmetic number type |
|---|
Parameters
| x | one number |
|---|---|
| x | the other number |
Definition at line 226 of file base_math.hpp.
◆ clamp() [1/3]
| T jau::clamp ( const T x, const T min_val, const T max_val ) | constexprnoexcept |
|---|
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1).
Implementation returns min(max(x, min_val), max_val), analog to GLSL's clamp()
Template Parameters
| T | an arithmetic number type |
|---|
Parameters
| x | one number |
|---|---|
| min_val | the minimum limes, inclusive |
| max_val | the maximum limes, inclusive |
Definition at line 242 of file base_math.hpp.
◆ clampCast()
| R jau::clampCast ( const T x, const T min_val, const T max_val ) | constexprnoexcept |
|---|
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1), cast to type R.
Implementation returns min(max(x, min_val), max_val), analog to GLSL's clamp()
Template Parameters
| R | an arithmetic number type, target return type |
|---|---|
| T | an arithmetic number type |
Parameters
| x | one number |
|---|---|
| min_val | the minimum limes, inclusive |
| max_val | the maximum limes, inclusive |
Definition at line 260 of file base_math.hpp.
◆ set_bit_uint32()
| void jau::set_bit_uint32 ( const uint8_t nr, uint32_t & mask ) | inline |
|---|
◆ clear_bit_uint32()
| void jau::clear_bit_uint32 ( const uint8_t nr, uint32_t & mask ) | inline |
|---|
◆ test_bit_uint32()
| uint32_t jau::test_bit_uint32 ( const uint8_t nr, const uint32_t mask ) | inline |
|---|
◆ set_bit_uint64()
| void jau::set_bit_uint64 ( const uint8_t nr, uint64_t & mask ) | inline |
|---|
◆ clear_bit_uint64()
| void jau::clear_bit_uint64 ( const uint8_t nr, uint64_t & mask ) | inline |
|---|
◆ test_bit_uint64()
| uint64_t jau::test_bit_uint64 ( const uint8_t nr, const uint64_t mask ) | inline |
|---|
◆ merge_uint128() [1/2]
| uint128dp_t jau::merge_uint128 | ( | uint16_t const | uuid16, |
|---|---|---|---|
| uint128dp_t const & | base_uuid, | ||
| nsize_t const | uuid16_le_octet_index ) |
Merge the given 'uuid16' into a 'base_uuid' copy at the given little endian 'uuid16_le_octet_index' position.
The given 'uuid16' value will be added with the 'base_uuid' copy at the given position.
base_uuid: 00000000-0000-1000-8000-00805F9B34FB uuid16: DCBA uuid16_le_octet_index: 12 result: 0000DCBA-0000-1000-8000-00805F9B34FB
LE: low-mem - FB349B5F8000-0080-0010-0000-ABCD0000 - high-mem ^ index 12 LE: uuid16 -> value.data[12+13]
BE: low-mem - 0000DCBA-0000-1000-8000-00805F9B34FB - high-mem ^ index 2 BE: uuid16 -> value.data[2+3]
Definition at line 493 of file basic_types.cpp.
◆ merge_uint128() [2/2]
| uint128dp_t jau::merge_uint128 | ( | uint32_t const | uuid32, |
|---|---|---|---|
| uint128dp_t const & | base_uuid, | ||
| nsize_t const | uuid32_le_octet_index ) |
Merge the given 'uuid32' into a 'base_uuid' copy at the given little endian 'uuid32_le_octet_index' position.
The given 'uuid32' value will be added with the 'base_uuid' copy at the given position.
base_uuid: 00000000-0000-1000-8000-00805F9B34FB uuid32: 87654321 uuid32_le_octet_index: 12 result: 87654321-0000-1000-8000-00805F9B34FB
LE: low-mem - FB349B5F8000-0080-0010-0000-12345678 - high-mem ^ index 12 LE: uuid32 -> value.data[12..15]
BE: low-mem - 87654321-0000-1000-8000-00805F9B34FB - high-mem ^ index 0 BE: uuid32 -> value.data[0..3]
Definition at line 527 of file basic_types.cpp.
◆ is_zero()
template<std::integral T>
| bool jau::is_zero ( const T & a) | constexprnoexcept |
|---|
base_math: arithmetic types, i.e.
integral + floating point types int_math: integral types float_math: floating point types Returns true of the given integer value is zero.
Definition at line 57 of file int_math.hpp.
◆ equals()
template<std::integral T>
| bool jau::equals ( const T & a, const T & b ) | constexprnoexcept |
|---|
Returns true if both values are equal.
Template Parameters
Parameters
| a | value to compare |
|---|---|
| b | value to compare |
Definition at line 69 of file int_math.hpp.
◆ round_up()
| T jau::round_up ( const T n, const U align_to ) | constexpr |
|---|
Round up w/ branching in O(1)
Template Parameters
| T | an unsigned integral number type |
|---|---|
| U | an unsigned integral number type |
Parameters
| n | to be aligned number |
|---|---|
| align_to | alignment boundary, must not be 0 |
Returns
n rounded up to a multiple of align_to
Definition at line 96 of file int_math.hpp.
◆ round_down()
| T jau::round_down ( T n, U align_to ) | constexpr |
|---|
Round down w/ branching in O(1)
Template Parameters
| T | an unsigned integral number type |
|---|---|
| U | an unsigned integral number type |
Parameters
| n | to be aligned number |
|---|---|
| align_to | alignment boundary |
Returns
n rounded down to a multiple of align_to
Definition at line 116 of file int_math.hpp.
◆ is_power_of_2()
| bool jau::is_power_of_2 ( const T x) | constexprnoexcept |
|---|
Power of 2 test in O(1), i.e.
test whether a single bit is set
Uses either
- C++20: std::has_single_bit
- else: bithacks Test PowerOf2
- Branching may occur due to relational operator.
Template Parameters
| T | an unsigned integral number type |
|---|
Parameters
| x | the unsigned integral number |
|---|
Returns
true if arg is 2^n for some x > 0
See also
std::has_single_bit()
Definition at line 134 of file int_math.hpp.
◆ log2_byteshift()
| size_t jau::log2_byteshift ( const size_t bytesize) | constexprnoexcept |
|---|
Returns log2(bytesize*8), e.g.
to bit-shift whole byte values
If bytesize is not of power2, zero is returned.
Parameters
Returns
log2(bytesize*8)
Definition at line 150 of file int_math.hpp.
◆ bit_ceil()
| T jau::bit_ceil ( const T n) | constexprnoexcept |
|---|
If the given n is not is_power_of_2() return next_power_of_2(), otherwise return n unchanged.
Uses either
- C++20: std::bit_ceil
- else: is_power_of_2 and ct_next_power_of_2
See also
std::bit_ceil()
Definition at line 186 of file int_math.hpp.
◆ high_bit()
| nsize_t jau::high_bit ( T x) | constexpr |
|---|
Return the index of the highest set bit w/ branching (loop) in O(n/2).
Template Parameters
| T | an unsigned integral number type |
|---|
Parameters
Definition at line 203 of file int_math.hpp.
◆ bit_count()
| size_t jau::bit_count ( T n) | constexprnoexcept |
|---|
Returns the number of set bits within given unsigned integral.
Template Parameters
| T | an unsigned integral number type |
|---|
Parameters
Definition at line 220 of file int_math.hpp.
◆ has_builtin_add_overflow()
Query whether __builtin_add_overflow is available via __has_builtin(__builtin_add_overflow).
Definition at line 228 of file int_math.hpp.
◆ has_builtin_sub_overflow()
Query whether __builtin_sub_overflow is available via __has_builtin(__builtin_sub_overflow).
Definition at line 239 of file int_math.hpp.
◆ has_builtin_mul_overflow()
Query whether __builtin_mul_overflow is available via __has_builtin(__builtin_mul_overflow).
Definition at line 250 of file int_math.hpp.
◆ add_overflow()
template<std::integral T>
| bool jau::add_overflow ( const T a, const T b, T & res ) | constexprnoexcept |
|---|
Integer overflow aware addition returning true if overflow occurred, otherwise false having the result stored in res.
Implementation uses Integer Overflow Builtins if available, otherwise its own implementation.
Template Parameters
| T | an integral integer type |
|---|
param a operand a
Parameters
| b | operand b |
|---|---|
| res | storage for result |
Returns
true if overflow, otherwise false
Definition at line 273 of file int_math.hpp.
◆ sub_overflow()
template<std::integral T>
| bool jau::sub_overflow ( const T a, const T b, T & res ) | constexprnoexcept |
|---|
Integer overflow aware subtraction returning true if overflow occurred, otherwise false having the result stored in res.
Implementation uses Integer Overflow Builtins if available, otherwise its own implementation.
Template Parameters
| T | an integral integer type |
|---|
param a operand a
Parameters
| b | operand b |
|---|---|
| res | storage for result |
Returns
true if overflow, otherwise false
Definition at line 304 of file int_math.hpp.
◆ mul_overflow()
template<std::integral T>
| bool jau::mul_overflow ( const T a, const T b, T & res ) | constexprnoexcept |
|---|
Integer overflow aware multiplication returning true if overflow occurred, otherwise false having the result stored in res.
Implementation uses Integer Overflow Builtins if available, otherwise its own implementation.
Template Parameters
| T | an integral integer type |
|---|
param a operand a
Parameters
| b | operand b |
|---|---|
| res | storage for result |
Returns
true if overflow, otherwise false
Definition at line 335 of file int_math.hpp.
◆ gcd() [1/2]
Returns the greatest common divisor (GCD) of the two given integer values following Euclid's algorithm from Euclid's Elements ~300 BC, using the absolute positive value of given integers.
Returns zero if a and b is zero.
Note implementation uses modulo operator (a/b)*b + a % b = a, i.e. remainder of the integer division - hence implementation uses abs(a) % abs(b) in case the integral T is a signed type (dropped for unsigned).
Implementation is similar to std::gcd(), however, it uses a fixed common type T and a while loop instead of compile time evaluation via recursion.
Template Parameters
param a integral value a
Parameters
Returns
zero if a and b are zero, otherwise the greatest common divisor (GCD) of a and b,
Definition at line 370 of file int_math.hpp.
◆ lcm_overflow()
template<std::integral T>
| bool jau::lcm_overflow ( const T a, const T b, T & result ) | constexprnoexcept |
|---|
Integer overflow aware calculation of least common multiple (LCM) following Euclid's algorithm from Euclid's Elements ~300 BC.
Template Parameters
param result storage for lcm result: zero if a and b are zero, otherwise lcm of a and b
Parameters
| a | integral value a |
|---|---|
| b | integral value b |
Returns
true if overflow, otherwise false for success
Definition at line 401 of file int_math.hpp.
◆ digits10() [1/3]
template<std::integral T>
| uint32_t jau::digits10 ( const T x, const int x_sign, const bool sign_is_digit = true ) | constexprnoexcept |
|---|
Returns the number of decimal digits of the given integral value number using std::log10().
If sign_is_digit == true (default), treats a negative sign of value x as a digit.
x < 0: 1 + (int) ( log10( -x ) ) + ( sign_is_digit ? 1 : 0 ) x = 0: 1 x > 0: 1 + (int) ( log10( x ) )
Implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
Convenience method, reusing precomputed sign of value to avoid redundant computations.
Template Parameters
| T | an integral integer type |
|---|
Parameters
| x | the integral integer |
|---|---|
| x_sign | the pre-determined sign of the given value x |
| sign_is_digit | if true and value is negative, adds one to result for sign. Defaults to true. |
Returns
digit count
Examples
Definition at line 436 of file int_math.hpp.
◆ digits10() [2/3]
template<std::integral T>
| uint32_t jau::digits10 ( const T x, const bool sign_is_digit ) | constexprnoexcept |
|---|
Returns the number of decimal digits of the given integral value number using std::log10().
If sign_is_digit == true (default), treats a negative sign of value x as a digit.
x < 0: 1 + (int) ( log10( -x ) ) + ( sign_is_digit ? 1 : 0 ) x = 0: 1 x > 0: 1 + (int) ( log10( x ) )
Implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
Template Parameters
| T | an integral integer type |
|---|
Parameters
| x | the integral integer |
|---|---|
| sign_is_digit | if true and value is negative, adds one to result for sign. Default is true. |
Returns
digit count
Definition at line 462 of file int_math.hpp.
◆ digits10() [3/3]
| uint32_t jau::digits10 ( const T x) | constexprnoexcept |
|---|
Returns the number of decimal digits of the given signed integral value number using std::log10() including a negative sign of value x as a digit.
Returns the number of decimal digits of the given unsigned integral value number using std::log10().
x < 0: 1 + (int) ( log10( -x ) ) + 1 x = 0: 1 x > 0: 1 + (int) ( log10( x ) )
Implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
Template Parameters
| T | a signed integral integer type |
|---|
Parameters
| x | the signed integral integer |
|---|
Returns
digit count
x = 0: 1 x > 0: 1 + (int) ( log10( x ) )
Implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
Template Parameters
| T | an unsigned integral integer type |
|---|
Parameters
| x | the unsigned integral integer |
|---|
Returns
digit count
Definition at line 480 of file int_math.hpp.
◆ digits()
| uint32_t jau::digits ( const T x, const uint32_t radix ) | constexprnoexcept |
|---|
Returns the number of digits of the given unsigned integral value number and the given radix.
Template Parameters
| T | an integral unsigned integer type |
|---|
Parameters
| x | the integral integer |
|---|---|
| radix | base of the number system, e.g. 2 binary, 8 octal, 10 decimal, 16 hexadecimal, .. |
Returns
digit count
Definition at line 513 of file int_math.hpp.