A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove

Rounded integral division

Rounded division algorithms for integral values.
20.43.0

These algorithms perform integer division while rounding the result as though the fractional part were actually present.
They do not check for a denominator of zero.
____________________________________________________________________________________________________
divide_round_to_ceiling
Values are rounded to the next more positive integral value.

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_to_ceiling(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  2
.150 / 100 ==  2
.149 / 100 ==  2
.51 / 100 ==  1
..50 / 100 ==  1
..49 / 100 ==  1
.-49 / 100 ==  0
.-50 / 100 ==  0
.-51 / 100 ==  0
-149 / 100 == -1
-150 / 100 == -1
-151 / 100 == -1
____________________________________________________________________________________________________
divide_round_to_floor
Values are rounded to the next more negative integral value.

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_to_floor(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  1
.150 / 100 ==  1
.149 / 100 ==  1
..51 / 100 ==  0
..50 / 100 ==  0
..49 / 100 ==  0
.-49 / 100 == -1
.-50 / 100 == -1
.-51 / 100 == -1
-149 / 100 == -2
-150 / 100 == -2
-151 / 100 == -2
____________________________________________________________________________________________________
divide_round_to_zero
Values are rounded towards zero.

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_to_zero(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  1
.150 / 100 ==  1
.149 / 100 ==  1
..51 / 100 ==  0
..50 / 100 ==  0
..49 / 100 ==  0
.-49 / 100 ==  0
.-50 / 100 ==  0
.-51 / 100 ==  0
-149 / 100 == -1
-150 / 100 == -1
-151 / 100 == -1
____________________________________________________________________________________________________
divide_round_to_infinity
Values are rounded towards infinity.

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_to_infinity(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  2
.150 / 100 ==  2
.149 / 100 ==  2
..51 / 100 ==  1
..50 / 100 ==  1
..49 / 100 ==  1
.-49 / 100 == -1
.-50 / 100 == -1
.-51 / 100 == -1
-149 / 100 == -2
-150 / 100 == -2
-151 / 100 == -2
____________________________________________________________________________________________________
divide_round_half_up
Values are rounded to the nearest integral value. 'Half' values are rounded up (towards infinity).

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_half_up(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  3
.150 / 100 ==  3
.149 / 100 ==  1
..51 / 100 ==  1
..50 / 100 ==  1
..49 / 100 ==  0
.-49 / 100 ==  0
.-50 / 100 == -1
.-51 / 100 == -1
-149 / 100 == -1
-150 / 100 == -3
-151 / 100 == -3
____________________________________________________________________________________________________
divide_round_half_down
Values are rounded to the nearest integral value. 'Half' values are rounded up (towards zero)

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_half_down(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  3
.150 / 100 ==  2
.149 / 100 ==  1
..51 / 100 ==  1
..50 / 100 ==  0
..49 / 100 ==  0
.-49 / 100 ==  0
.-50 / 100 ==  0
.-51 / 100 == -1
-149 / 100 == -1
-150 / 100 == -2
-151 / 100 == -3
____________________________________________________________________________________________________
divide_round_half_even
(Banker's rounding)
Values are rounded to the nearest integral value. 'Half' values are rounded to the nearest even value.

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_half_even(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  2
.150 / 100 ==  2
.149 / 100 ==  1
..51 / 100 ==  1
..50 / 100 ==  0
..49 / 100 ==  0
.-49 / 100 ==  0
.-50 / 100 ==  0
.-51 / 100 == -1
-149 / 100 == -1
-150 / 100 == -2
-151 / 100 == -2
____________________________________________________________________________________________________
divide_round_half_odd
Values are rounded to the nearest integral value. 'Half' values are rounded to the nearest odd value.

template <typename T1, typename T2>
ETL_CONSTEXPR14
etl::common_type_t<T1, T2>
divide_round_half_even(T1 numerator, T2 denominator) ETL_NOEXCEPT

.151 / 100 ==  2
.150 / 100 ==  1
.149 / 100 ==  1
..51 / 100 ==  1
..50 / 100 ==  1
..49 / 100 ==  0
.-49 / 100 ==  0
.-50 / 100 == -1
.-51 / 100 == -1
-149 / 100 == -1
-150 / 100 == -1
-151 / 100 == -2
rounded_integral_division.h