Arduino Helpers master
Utility library for Arduino
Divide.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <AH/STL/cstdint>
4#include <AH/STL/type_traits>
6#include <stddef.h>
7
9
13template <uint8_t N, class T>
15 static T div(T val) { return val / N; }
16};
17
19template <uint8_t N, class T>
21 static T div(T val) {
22 return (val + (N / 2)) / N;
23 static_assert(std::is_unsigned<T>::value && std::is_integral<T>::value,
24 "This function is only valid for unsigned integers");
25 }
26};
27
29template <uint8_t N, class T>
31 static T div(T val) {
32 T offset = val >= 0 ? (N / 2) : (-N / 2);
33 return (val + offset) / N;
34 }
35};
36
39template <uint8_t N, class T>
41 : std::conditional<std::is_signed<T>::value, round_div_signed_int<N, T>,
42 round_div_unsigned_int<N, T>>::type {};
43
46template <uint8_t N, class T>
48 : std::conditional<std::is_integral<T>::value, round_div_int<N, T>,
49 round_div_default<N, T>>::type {};
50
53template <size_t N, class T>
54T round_div(T val) {
56}
57
T round_div(T val)
Divide a number by N and round the result.
Definition: Divide.hpp:54
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Divide by N using the default division operator, without explicit rounding This should be used for fl...
Definition: Divide.hpp:14
static T div(T val)
Definition: Divide.hpp:15
Select the right rounding division operator, depending on whether T is an integer or not.
Definition: Divide.hpp:49
Select the right rounding division operator, depending on whether T is a signed or unsigned integer.
Definition: Divide.hpp:42
Divide a signed integer by N, rounding the result.
Definition: Divide.hpp:30
static T div(T val)
Definition: Divide.hpp:31
Divide an unsigned integer by N, rounding the result.
Definition: Divide.hpp:20
static T div(T val)
Definition: Divide.hpp:21