Arduino Helpers master
Utility library for Arduino
Modules | Functions
Math Utilities

Detailed Description

Mathematics helper functions.

Min/max functions, functions to uniformly increase the effective bit depth, etc.

+ Collaboration diagram for Math Utilities:

Modules

 Math Types
 Vector and Quaternion types with the necessary operators and functions.
 

Functions

template<class T >
constexpr std::enable_if< std::is_floating_point< T >::value, T >::type rad2deg (T r)
 Convert radians to degrees. More...
 
template<class T >
constexpr std::enable_if< std::is_floating_point< T >::value, T >::type deg2rad (T d)
 Convert degrees to radians. More...
 
template<size_t Bits_out, size_t Bits_in, class T_out , class T_in >
T_out increaseBitDepth (T_in in)
 Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide, (approximately) evenly distributing the error across the entire range, such that the error for each element is between -1 and +1. More...
 
template<size_t Bits_out, size_t Bits_in, class T_out , class T_in >
T_out increaseBitDepthMiddle (T_in in)
 Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide, while ensuring that the middle of the input range maps exactly to the middle of the output range, i.e. More...
 
template<class T , class U >
constexpr auto min (const T &a, const U &b) -> decltype(b< a ? b :a)
 Return the smaller of two numbers/objects. More...
 
template<class T , class U >
constexpr auto max (const T &a, const U &b) -> decltype(a< b ? b :a)
 Return the larger of two numbers/objects. More...
 

Function Documentation

◆ rad2deg()

constexpr std::enable_if< std::is_floating_point< T >::value, T >::type rad2deg ( r)
inlineconstexpr

Convert radians to degrees.

Definition at line 24 of file Degrees.hpp.

◆ deg2rad()

constexpr std::enable_if< std::is_floating_point< T >::value, T >::type deg2rad ( d)
inlineconstexpr

Convert degrees to radians.

Definition at line 31 of file Degrees.hpp.

◆ increaseBitDepth()

T_out increaseBitDepth ( T_in  in)

Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide, (approximately) evenly distributing the error across the entire range, such that the error for each element is between -1 and +1.

See also
increaseBitDepthMiddle

For example, converting 3-bit numbers to 7-bit numbers would result in the following:

in (dec) in (bin) out (bin) out (dec) exact error
0 000 000'0000 0 0.00 +0.00
1 001 001'0010 18 18.14 +0.14
2 010 010'0100 36 36.29 +0.29
3 011 011'0110 54 54.43 +0.43
4 100 100'1001 73 72.57 -0.43
5 101 101'1011 91 90.71 -0.29
6 110 110'1101 109 108.86 -0.14
7 111 111'1111 127 127.00 +0.00

The following is a comparison to the increaseBitDepthMiddle function.

Template Parameters
Bits_outThe number of bits of the output range.
Bits_inThe number of bits of the input range.
T_outThe type of the output (return type).
T_inThe type of the input.
Parameters
inThe value to scale up.
Returns
The scaled up value.

Definition at line 77 of file IncreaseBitDepth.hpp.

◆ increaseBitDepthMiddle()

T_out increaseBitDepthMiddle ( T_in  in)

Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide, while ensuring that the middle of the input range maps exactly to the middle of the output range, i.e.

\( 2^{\texttt{Bits\_in} - 1} \) maps to \( 2^{\texttt{Bits\_out} - 1} \).

See also
increaseBitDepth

For example, converting 3-bit numbers to 7-bit numbers would result in the following:

in (dec) in (bin) out (bin) out (dec) exact error
0 000 000'0000 0 0.00 +0.00
1 001 001'0000 16 18.14 -2.14
2 010 010'0000 32 36.29 -4.29
3 011 011'0000 48 54.43 -6.43
4 100 100'0000 64 72.57 -8.57
5 101 101'0101 85 90.71 -5.71
6 110 110'1010 106 108.86 -2.86
7 111 111'1111 127 127.00 +0.00

The following is a comparison to the increaseBitDepth function.

Template Parameters
Bits_outThe number of bits of the output range.
Bits_inThe number of bits of the input range.
T_outThe type of the output (return type).
T_inThe type of the input.
Parameters
inThe value to scale up.
Returns
The scaled up value.

Definition at line 125 of file IncreaseBitDepth.hpp.

◆ min()

constexpr auto min ( const T &  a,
const U &  b 
) -> decltype(b < a ? b : a)
constexpr

Return the smaller of two numbers/objects.

Examples
Blink-Frequency-Buttons.ino.

Definition at line 12 of file MinMaxFix.hpp.

◆ max()

constexpr auto max ( const T &  a,
const U &  b 
) -> decltype(a < b ? b : a)
constexpr

Return the larger of two numbers/objects.

Examples
Blink-Frequency-Buttons.ino.

Definition at line 19 of file MinMaxFix.hpp.