pw_numeric#

Efficient mathematical utilities for embedded

Stable C++

pw_numeric is a collection of mathematical utilities optimized for embedded systems.

What belongs in this module?

Any standalone mathematical, numerical, or statisitical utilities that are optimized for embedded belong in pw_numeric. This encompasses a broad area, but since the C++ standard library provides many mathematical utilities already, this module is not anticipated to be too large. If the module does grow substantially, some features may be moved to other modules (e.g. pw_statistics).

What does NOT belong in this module?

  • Features available in the C++ standard library (<cmath>, <numeric>, <bit>, etc.) should not be duplicated.

  • Pseudo-random number generation belongs in pw_random.

  • Data integrity checking belongs in pw_checksum.

  • Bit / byte manipulation belong in pw_bytes.

C++ API reference#

pw_numeric/integer_division.h#

template<typename T>
constexpr T pw::IntegerDivisionRoundNearest(T dividend, T divisor)#

Performs integer division and rounds to the nearest integer. Gives the same result as std::round(static_cast<double>(dividend) / static_cast<double>(divisor)), but requires no floating point operations and is constexpr.

Warning

signed or unsigned int, long, or long long operands overflow if:

  • the quotient is positive and dividend + divisor/2 overflows, or

  • the quotient is negative and dividend - divisor/2 overflows.

To avoid overflow, do not use this function with very large operands, or cast int or long to a larger type first. Overflow cannot occur with types smaller than int because C++ implicitly converts them to int.