jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
Integer types and arithmetic

Integral integer types and arithmetic. More...

Namespaces

namespace  jau::int_literals
 

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< 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< 4 >
 
struct  jau::uint_bytes< 8 >
 

Typedefs

template<int bytesize>
using jau::float_bytes_t = typename float_bytes<bytesize>::type
 Alias template for float_bytes.
 
typedef uint_fast32_t jau::nsize_t
 Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
 
template<int bytesize>
using jau::sint_bytes_t = typename sint_bytes<bytesize>::type
 Alias template for sint_bytes.
 
typedef int_fast32_t jau::snsize_t
 Natural 'ssize_t' alternative using int_fast32_t as its natural sized type.
 
template<int bytesize>
using jau::uint_bytes_t = typename uint_bytes<bytesize>::type
 Alias template for uint_bytes.
 

Functions

mp::BigInt jau::abs (const mp::BigInt &x) noexcept
 
template<typename T, std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true>
constexpr T jau::abs (const T x) noexcept
 Returns the absolute value of an arithmetic number (w/ branching) in O(1)
 
template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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.
 
const mp::BigIntjau::clamp (const mp::BigInt &x, const mp::BigInt &min_val, const mp::BigInt &max_val) noexcept
 
template<typename T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
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::BigIntjau::clamp (mp::BigInt &x, mp::BigInt &min_val, mp::BigInt &max_val) noexcept
 
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<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
constexpr nsize_t jau::digits10 (const T x, const bool sign_is_digit=true) noexcept
 Returns the number of decimal digits of the given integral value number using std::log10<T>().
 
template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
constexpr nsize_t jau::digits10 (const T x, const snsize_t x_sign, const bool sign_is_digit=true) noexcept
 Returns the number of decimal digits of the given integral value number using std::log10<T>().
 
template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::equals (const T &a, const T &b) noexcept
 Returns true if both values are equal.
 
template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::equals (const T &a, const T &b, const T &allowed_deviation) noexcept
 Returns true if both values are equal, i.e.
 
mp::BigInt jau::gcd (const mp::BigInt &a, const mp::BigInt &b) noexcept
 
template<typename T, std::enable_if_t< std::is_integral_v< T > &&!std::is_unsigned_v< T >, bool > = true>
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<typename T, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
constexpr nsize_t jau::high_bit (T x)
 Return the index of the highest set bit w/ branching (loop) in O(n), actually O(n/2).
 
template<class T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
bool jau::in_range (const T &a, const T &b, const T &range)
 base_math: arithmetic types, i.e.
 
template<typename T, std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true>
constexpr T jau::invert_sign (const T x) noexcept
 Safely inverts the sign of an arithmetic number w/ branching in O(1)
 
template<typename T, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
constexpr bool jau::is_power_of_2 (const T x) noexcept
 Power of 2 test (w/o branching ?) in O(1)
 
template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::is_zero (const T &a) noexcept
 base_math: arithmetic types, i.e.
 
template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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.
 
const mp::BigIntjau::max (const mp::BigInt &x, const mp::BigInt &y) noexcept
 
template<typename T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
constexpr T jau::max (const T x, const T y) noexcept
 Returns the maximum of two integrals (w/ branching) in O(1)
 
mp::BigIntjau::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::BigIntjau::min (const mp::BigInt &x, const mp::BigInt &y) noexcept
 
template<typename T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
constexpr T jau::min (const T x, const T y) noexcept
 Returns the minimum of two integrals (w/ branching) in O(1)
 
mp::BigIntjau::min (mp::BigInt &x, mp::BigInt &y) noexcept
 
template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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<typename T, typename U, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T > &&std::is_integral_v< U > &&std::is_unsigned_v< U >, bool > = true>
constexpr T jau::round_down (T n, U align_to)
 Round down w/ branching in O(1)
 
constexpr uint32_t jau::round_to_power_of_2 (const uint32_t n)
 If the given n is not is_power_of_2() return next_power_of_2(), otherwise return n unchanged.
 
template<typename T, typename U, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T > &&std::is_integral_v< U > &&std::is_unsigned_v< U >, bool > = true>
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<typename T, std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true>
constexpr int jau::sign (const T x) noexcept
 Returns the value of the sign function (w/o branching ?) in O(1).
 
template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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)
 

Detailed Description

Integral integer types and arithmetic.

Further support is coming from Byte Utilities and meta-group Math Support

Typedef Documentation

◆ nsize_t

typedef uint_fast32_t jau::nsize_t

Natural 'size_t' alternative using uint_fast32_t as its natural sized type.

The leading 'n' stands for natural.

This is a compromise to indicate intend, but to avoid handling a multiple sized size_t footprint where not desired.

Examples
test_lfringbuffer11.cpp.

Definition at line 55 of file int_types.hpp.

◆ snsize_t

typedef int_fast32_t jau::snsize_t

Natural 'ssize_t' alternative using int_fast32_t as its natural sized type.

The leading 'n' stands for natural.

This is a compromise to indicate intend, but to avoid handling a multiple sized ssize_t footprint where not desired.

Definition at line 67 of file int_types.hpp.

◆ uint_bytes_t

template<int bytesize>
using jau::uint_bytes_t = typename uint_bytes<bytesize>::type

Alias template for uint_bytes.

Definition at line 79 of file int_types.hpp.

◆ sint_bytes_t

template<int bytesize>
using jau::sint_bytes_t = typename sint_bytes<bytesize>::type

Alias template for sint_bytes.

Definition at line 89 of file int_types.hpp.

◆ float_bytes_t

template<int bytesize>
using jau::float_bytes_t = typename float_bytes<bytesize>::type

Alias template for float_bytes.

Definition at line 97 of file int_types.hpp.

Function Documentation

◆ in_range()

template<class T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
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
Tan arithmetic type
Parameters
avalue to compare
bvalue to compare
rangethe maximum difference both values may differ

Definition at line 62 of file base_math.hpp.

◆ sign()

template<typename T, std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true>
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
Tan arithmetic number type
Parameters
xthe arithmetic number
Returns
function result

Definition at line 84 of file base_math.hpp.

Here is the caller graph for this function:

◆ invert_sign()

template<typename T, std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true>
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<T>::min(), converted to T_MAX, i.e. std::numeric_limits<T>::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_t>(INT32_MIN) = | INT32_MIN | - 1 = INT32_MAX

Otherwise with x < 0:

jau::invert_sign<int32_t>(x) = | x | = -x

and x >= 0:

jau::invert_sign<int32_t>(x) = -x
Template Parameters
Tan unsigned arithmetic number type
Parameters
xthe number
Returns
function result

Definition at line 126 of file base_math.hpp.

Here is the caller graph for this function:

◆ abs() [1/2]

template<typename T, std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true>
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
Tan arithmetic number type
Parameters
xthe number
Returns
function result

Definition at line 155 of file base_math.hpp.

Here is the caller graph for this function:

◆ min() [1/3]

template<typename T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
T jau::min ( const T x,
const T y )
constexprnoexcept

Returns the minimum of two integrals (w/ branching) in O(1)

Template Parameters
Tan arithmetic number type
Parameters
xone number
xthe other number

Definition at line 177 of file base_math.hpp.

Here is the caller graph for this function:

◆ max() [1/3]

template<typename T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
T jau::max ( const T x,
const T y )
constexprnoexcept

Returns the maximum of two integrals (w/ branching) in O(1)

Template Parameters
Tan arithmetic number type
Parameters
xone number
xthe other number

Definition at line 191 of file base_math.hpp.

Here is the caller graph for this function:

◆ clamp() [1/3]

template<typename T, std::enable_if_t< std::is_arithmetic_v< T >, bool > = true>
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
Tan arithmetic number type
Parameters
xone number
min_valthe minimum limes, inclusive
max_valthe maximum limes, inclusive

Definition at line 208 of file base_math.hpp.

Here is the caller graph for this function:

◆ set_bit_uint32()

void jau::set_bit_uint32 ( const uint8_t nr,
uint32_t & mask )
inline

Definition at line 559 of file basic_types.hpp.

◆ clear_bit_uint32()

void jau::clear_bit_uint32 ( const uint8_t nr,
uint32_t & mask )
inline

Definition at line 566 of file basic_types.hpp.

◆ test_bit_uint32()

uint32_t jau::test_bit_uint32 ( const uint8_t nr,
const uint32_t mask )
inline

Definition at line 573 of file basic_types.hpp.

◆ set_bit_uint64()

void jau::set_bit_uint64 ( const uint8_t nr,
uint64_t & mask )
inline

Definition at line 580 of file basic_types.hpp.

◆ clear_bit_uint64()

void jau::clear_bit_uint64 ( const uint8_t nr,
uint64_t & mask )
inline

Definition at line 587 of file basic_types.hpp.

◆ test_bit_uint64()

uint64_t jau::test_bit_uint64 ( const uint8_t nr,
const uint64_t mask )
inline

Definition at line 594 of file basic_types.hpp.

◆ 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 528 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 563 of file basic_types.cpp.

◆ is_zero()

template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr 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 56 of file int_math.hpp.

◆ equals() [1/2]

template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::equals ( const T & a,
const T & b )
constexprnoexcept

Returns true if both values are equal.

Template Parameters
Tan integral type
Parameters
avalue to compare
bvalue to compare

Definition at line 69 of file int_math.hpp.

◆ equals() [2/2]

template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::equals ( const T & a,
const T & b,
const T & allowed_deviation )
constexprnoexcept

Returns true if both values are equal, i.e.

their absolute delta <= allowed_deviation.

Template Parameters
Tan integral type
Parameters
avalue to compare
bvalue to compare
allowed_deviationallowed deviation

Definition at line 83 of file int_math.hpp.

◆ round_up()

template<typename T, typename U, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T > &&std::is_integral_v< U > &&std::is_unsigned_v< U >, bool > = true>
T jau::round_up ( const T n,
const U align_to )
constexpr

Round up w/ branching in O(1)

Template Parameters
Tan unsigned integral number type
Uan unsigned integral number type
Parameters
nto be aligned number
align_toalignment boundary, must not be 0
Returns
n rounded up to a multiple of align_to

Definition at line 99 of file int_math.hpp.

Here is the caller graph for this function:

◆ round_down()

template<typename T, typename U, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T > &&std::is_integral_v< U > &&std::is_unsigned_v< U >, bool > = true>
T jau::round_down ( T n,
U align_to )
constexpr

Round down w/ branching in O(1)

Template Parameters
Tan unsigned integral number type
Uan unsigned integral number type
Parameters
nto be aligned number
align_toalignment boundary
Returns
n rounded down to a multiple of align_to

Definition at line 121 of file int_math.hpp.

Here is the caller graph for this function:

◆ is_power_of_2()

template<typename T, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
bool jau::is_power_of_2 ( const T x)
constexprnoexcept

Power of 2 test (w/o branching ?) in O(1)

Source: bithacks Test PowerOf2

Branching may occur due to relational operator.

Template Parameters
Tan unsigned integral number type
Parameters
xthe unsigned integral number
Returns
true if arg is 2^n for some n > 0

Definition at line 138 of file int_math.hpp.

Here is the caller graph for this function:

◆ round_to_power_of_2()

uint32_t jau::round_to_power_of_2 ( const uint32_t n)
constexpr

If the given n is not is_power_of_2() return next_power_of_2(), otherwise return n unchanged.

return is_power_of_2(n) ? n : next_power_of_2(n);

Definition at line 150 of file int_math.hpp.

Here is the caller graph for this function:

◆ high_bit()

template<typename T, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
nsize_t jau::high_bit ( T x)
constexpr

Return the index of the highest set bit w/ branching (loop) in O(n), actually O(n/2).

Template Parameters
Tan unsigned integral number type
Parameters
xvalue

Definition at line 162 of file int_math.hpp.

Here is the caller graph for this function:

◆ has_builtin_add_overflow()

consteval_cxx20 bool jau::has_builtin_add_overflow ( )
noexcept

Query whether __builtin_add_overflow is available via __has_builtin(__builtin_add_overflow).

Definition at line 177 of file int_math.hpp.

Here is the caller graph for this function:

◆ has_builtin_sub_overflow()

consteval_cxx20 bool jau::has_builtin_sub_overflow ( )
noexcept

Query whether __builtin_sub_overflow is available via __has_builtin(__builtin_sub_overflow).

Definition at line 188 of file int_math.hpp.

Here is the caller graph for this function:

◆ has_builtin_mul_overflow()

consteval_cxx20 bool jau::has_builtin_mul_overflow ( )
noexcept

Query whether __builtin_mul_overflow is available via __has_builtin(__builtin_mul_overflow).

Definition at line 199 of file int_math.hpp.

Here is the caller graph for this function:

◆ add_overflow()

template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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
Tan integral integer type

param a operand a

Parameters
boperand b
resstorage for result
Returns
true if overflow, otherwise false

Definition at line 223 of file int_math.hpp.

Here is the caller graph for this function:

◆ sub_overflow()

template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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
Tan integral integer type

param a operand a

Parameters
boperand b
resstorage for result
Returns
true if overflow, otherwise false

Definition at line 257 of file int_math.hpp.

Here is the caller graph for this function:

◆ mul_overflow()

template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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
Tan integral integer type

param a operand a

Parameters
boperand b
resstorage for result
Returns
true if overflow, otherwise false

Definition at line 291 of file int_math.hpp.

Here is the caller graph for this function:

◆ gcd() [1/2]

template<typename T, std::enable_if_t< std::is_integral_v< T > &&!std::is_unsigned_v< T >, bool > = true>
T jau::gcd ( T a,
T b )
constexprnoexcept

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
Tintegral type

param a integral value a

Parameters
bintegral value b
Returns
zero if a and b are zero, otherwise the greatest common divisor (GCD) of a and b,

Definition at line 330 of file int_math.hpp.

Here is the caller graph for this function:

◆ lcm_overflow()

template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
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
Tintegral type

param result storage for lcm result: zero if a and b are zero, otherwise lcm of a and b

Parameters
aintegral value a
bintegral value b
Returns
true if overflow, otherwise false for success

Definition at line 366 of file int_math.hpp.

Here is the caller graph for this function:

◆ digits10() [1/2]

template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
nsize_t jau::digits10 ( const T x,
const snsize_t x_sign,
const bool sign_is_digit = true )
constexprnoexcept

Returns the number of decimal digits of the given integral value number using std::log10<T>().


If sign_is_digit == true (default), treats a potential negative sign 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
Tan integral integer type
Parameters
xthe integral integer
x_signthe pre-determined sign of the given value x
sign_is_digitif true and value is negative, adds one to result for sign. Defaults to true.
Returns
digit count
Examples
test_intdecstring01.cpp.

Definition at line 403 of file int_math.hpp.

Here is the caller graph for this function:

◆ digits10() [2/2]

template<typename T, std::enable_if_t< std::is_integral_v< T >, bool > = true>
nsize_t jau::digits10 ( const T x,
const bool sign_is_digit = true )
constexprnoexcept

Returns the number of decimal digits of the given integral value number using std::log10<T>().

If sign_is_digit == true (default), treats a potential negative sign 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
Tan integral integer type
Parameters
xthe integral integer
sign_is_digitif true and value is negative, adds one to result for sign. Defaults to true.
Returns
digit count

Definition at line 431 of file int_math.hpp.

◆ abs() [2/2]

mp::BigInt jau::abs ( const mp::BigInt & x)
inlinenoexcept

Definition at line 1559 of file big_int.hpp.

◆ pow()

mp::BigInt jau::pow ( mp::BigInt b,
const mp::BigInt & e )
inline

Definition at line 1560 of file big_int.hpp.

Here is the caller graph for this function:

◆ min() [2/3]

const mp::BigInt & jau::min ( const mp::BigInt & x,
const mp::BigInt & y )
inlinenoexcept

Definition at line 1562 of file big_int.hpp.

◆ max() [2/3]

const mp::BigInt & jau::max ( const mp::BigInt & x,
const mp::BigInt & y )
inlinenoexcept

Definition at line 1565 of file big_int.hpp.

◆ clamp() [2/3]

const mp::BigInt & jau::clamp ( const mp::BigInt & x,
const mp::BigInt & min_val,
const mp::BigInt & max_val )
inlinenoexcept

Definition at line 1568 of file big_int.hpp.

◆ min() [3/3]

mp::BigInt & jau::min ( mp::BigInt & x,
mp::BigInt & y )
inlinenoexcept

Definition at line 1572 of file big_int.hpp.

◆ max() [3/3]

mp::BigInt & jau::max ( mp::BigInt & x,
mp::BigInt & y )
inlinenoexcept

Definition at line 1575 of file big_int.hpp.

◆ clamp() [3/3]

mp::BigInt & jau::clamp ( mp::BigInt & x,
mp::BigInt & min_val,
mp::BigInt & max_val )
inlinenoexcept

Definition at line 1578 of file big_int.hpp.

◆ gcd() [2/2]

mp::BigInt jau::gcd ( const mp::BigInt & a,
const mp::BigInt & b )
inlinenoexcept

Definition at line 1582 of file big_int.hpp.