bn::rule_of_three_approximation class

Approximates a rule of three cross-multiplication (x = (value * multiplier) / divisor).

rule_of_three_approximation objects should be created at compile time to improve performance.

See https://en.wikipedia.org/wiki/Cross-multiplication#Rule_of_Three

Constructors, destructors, conversion operators

rule_of_three_approximation(int divisor, int multiplier) constexpr
Constructor.

Public functions

auto calculate(unsigned value) const -> unsigned constexpr
Returns an approximation of (value * multiplier) / divisor, casting the value to uint64_t to try to avoid integer overflow.
auto unsafe_calculate(unsigned value) const -> unsigned constexpr
Returns an approximation of (value * multiplier) / divisor, without trying to avoid integer overflow.
auto calculate(int value) const -> int constexpr
Returns an approximation of (value * multiplier) / divisor, casting the value to int64_t to try to avoid integer overflow.
auto unsafe_calculate(int value) const -> int constexpr
Returns an approximation of (value * multiplier) / divisor, without trying to avoid integer overflow.
template<int Precision>
auto calculate(fixed_t<Precision> value) const -> fixed_t<Precision> constexpr
Returns an approximation of (value * multiplier) / divisor, casting the value to int64_t to try to avoid integer overflow.
template<int Precision>
auto unsafe_calculate(fixed_t<Precision> value) const -> fixed_t<Precision> constexpr
Returns an approximation of (value * multiplier) / divisor, without trying to avoid integer overflow.

Function documentation

bn::rule_of_three_approximation::rule_of_three_approximation(int divisor, int multiplier) constexpr

Constructor.

Parameters
divisor Valid divisor (> 0).
multiplier Valid multiplier (> 0).