Speeduino
Loading...
Searching...
No Matches
Modules | Macros | Functions
Rounding integer division

Integer division returns the quotient. I.e. rounds to zero. This code will round the result to nearest integer. Rounding behavior is controlled by DIV_ROUND_BEHAVIOR. More...

Collaboration diagram for Rounding integer division:

Modules

 Rounding behavior
 

Macros

#define DIV_ROUND_CLOSEST(n, d, t)
 Rounded integer division.
 
#define UDIV_ROUND_CLOSEST(n, d, t)   ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d))
 Rounded unsigned integer division.
 

Functions

template<uint16_t divisor>
static constexpr uint16_t div_round_closest_u16 (uint16_t n)
 Rounded unsigned integer division optimized for compile time constants.
 

Detailed Description

Integer division returns the quotient. I.e. rounds to zero. This code will round the result to nearest integer. Rounding behavior is controlled by DIV_ROUND_BEHAVIOR.

Macro Definition Documentation

◆ DIV_ROUND_CLOSEST

#define DIV_ROUND_CLOSEST (   n,
  d,
  t 
)
Value:
( \
(((n) < (t)(0)) ^ ((d) < (t)(0))) ? \
((t)((n) - DIV_ROUND_CORRECT(d, t))/(t)(d)) : \
((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d)))
#define DIV_ROUND_CORRECT(d, t)
Computes the denominator correction for rounding division based on our rounding behavior.
Definition maths.h:58
static TIntegral readSerialIntegralTimeout(void)
Reads an integral type, timing out if necessary.
Definition comms.cpp:173

Rounded integer division.

Integer division returns the quotient. I.e. rounds to zero. This macro will round the result to nearest integer. Rounding behavior is controlled by DIV_ROUND_BEHAVIOR

Warning
For performance reasons, this macro does not promote integers. So it will overflow if n>MAX(t)-(d/2).
Parameters
nThe numerator (dividee) (an integer)
dThe denominator (divider) (an integer)
tThe type of the result. E.g. uint16_t

◆ UDIV_ROUND_CLOSEST

#define UDIV_ROUND_CLOSEST (   n,
  d,
  t 
)    ((t)((n) + DIV_ROUND_CORRECT(d, t))/(t)(d))

Rounded unsigned integer division.

This is slightly faster than the signed version (DIV_ROUND_CLOSEST(n, d, t))

Warning
For performance reasons, this macro does not promote integers. So it will overflow if n>MAX(t)-(d/2).
Parameters
nThe numerator (dividee) (an unsigned integer)
dThe denominator (divider) (an unsigned integer)
tThe type of the result. E.g. uint16_t

Function Documentation

◆ div_round_closest_u16()

template<uint16_t divisor>
static constexpr uint16_t div_round_closest_u16 ( uint16_t  n)
staticconstexpr

Rounded unsigned integer division optimized for compile time constants.

Template Parameters
divisorDivisor
Parameters
nDividend
Returns
uint16_t
Here is the call graph for this function: