AP_Math: added unsigned versions of constrain functions
sometimes it really does matter that we use constrain_uint32() instead of constrain_int32(). For example, if we have a value like 0xFFFFFFFF then the result will be very different we should use unsigned constrain when dealing with unsigned values
This commit is contained in:
parent
82235163a2
commit
a95b429acc
@ -313,9 +313,13 @@ T constrain_value(const T amt, const T low, const T high)
|
||||
}
|
||||
|
||||
template int constrain_value<int>(const int amt, const int low, const int high);
|
||||
template unsigned int constrain_value<unsigned int>(const unsigned int amt, const unsigned int low, const unsigned int high);
|
||||
template long constrain_value<long>(const long amt, const long low, const long high);
|
||||
template unsigned long constrain_value<unsigned long>(const unsigned long amt, const unsigned long low, const unsigned long high);
|
||||
template long long constrain_value<long long>(const long long amt, const long long low, const long long high);
|
||||
template unsigned long long constrain_value<unsigned long long>(const unsigned long long amt, const unsigned long long low, const unsigned long long high);
|
||||
template short constrain_value<short>(const short amt, const short low, const short high);
|
||||
template unsigned short constrain_value<unsigned short>(const unsigned short amt, const unsigned short low, const unsigned short high);
|
||||
template float constrain_value<float>(const float amt, const float low, const float high);
|
||||
template double constrain_value<double>(const double amt, const double low, const double high);
|
||||
|
||||
|
@ -179,16 +179,31 @@ inline int16_t constrain_int16(const int16_t amt, const int16_t low, const int16
|
||||
return constrain_value(amt, low, high);
|
||||
}
|
||||
|
||||
inline uint16_t constrain_uint16(const uint16_t amt, const uint16_t low, const uint16_t high)
|
||||
{
|
||||
return constrain_value(amt, low, high);
|
||||
}
|
||||
|
||||
inline int32_t constrain_int32(const int32_t amt, const int32_t low, const int32_t high)
|
||||
{
|
||||
return constrain_value(amt, low, high);
|
||||
}
|
||||
|
||||
inline uint32_t constrain_uint32(const uint32_t amt, const uint32_t low, const uint32_t high)
|
||||
{
|
||||
return constrain_value(amt, low, high);
|
||||
}
|
||||
|
||||
inline int64_t constrain_int64(const int64_t amt, const int64_t low, const int64_t high)
|
||||
{
|
||||
return constrain_value(amt, low, high);
|
||||
}
|
||||
|
||||
inline uint64_t constrain_uint64(const uint64_t amt, const uint64_t low, const uint64_t high)
|
||||
{
|
||||
return constrain_value(amt, low, high);
|
||||
}
|
||||
|
||||
// degrees -> radians
|
||||
static inline constexpr ftype radians(ftype deg)
|
||||
{
|
||||
|
@ -383,6 +383,10 @@ TEST(MathTest, Constrain)
|
||||
EXPECT_EQ(19.9, constrain_value(19.8, 19.9, 20.1));
|
||||
EXPECT_EQ(19.9f, constrain_value(19.8f, 19.9f, 20.1f));
|
||||
|
||||
// test that constrain on 32 bit integer works correctly. Note the asymmetry
|
||||
EXPECT_EQ(10, constrain_int32( 0xFFFFFFFFU, 10U, 1200U));
|
||||
EXPECT_EQ(1200U, constrain_uint32(0xFFFFFFFFU, 10U, 1200U));
|
||||
|
||||
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
||||
EXPECT_EQ(1.0f, constrain_float(nanf("0x4152"), 1.0f, 1.0f));
|
||||
EXPECT_EQ(1.0f, constrain_value(nanf("0x4152"), 1.0f, 1.0f));
|
||||
|
Loading…
Reference in New Issue
Block a user