Speeduino
Loading...
Searching...
No Matches
Modules | Macros
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.
 

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)))
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:349
#define DIV_ROUND_CORRECT(d, t)
Computes the denominator correction for rounding division based on our rounding behavior.
Definition maths.h:59

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