maths: add 'frac' function

Extracts the fractional component from a floating point number. Prefer
to use temporaries that you have on hand over these functions.
This commit is contained in:
Danny Robson 2018-05-21 15:06:07 +10:00
parent 7f00f92e59
commit cdac2dcbc2
2 changed files with 34 additions and 1 deletions

View File

@ -1368,7 +1368,7 @@ namespace util {
template <
typename K,
typename = std::enable_if_t<
is_coord_v<K> && std::is_floating_point<typename K::value_type>::value, void
is_coord_v<K> && std::is_floating_point_v<typename K::value_type>
>
>
constexpr auto
@ -1388,6 +1388,28 @@ namespace util {
}
///------------------------------------------------------------------------
/// Return the fractional part of a real value.
///
/// This is an extraodinarily naive implementation. We avoid doing
/// explicit casts here in the hope that floor and sub is more efficient
/// (ie, keeps floats as floats in registers).
template <
typename K,
typename = std::enable_if_t<
is_coord_v<K> &&
std::is_floating_point_v<
typename K::value_type
>
>
>
constexpr auto
frac (const K k)
{
return k - floor (k);
}
///////////////////////////////////////////////////////////////////////////
/// shifts all elements `num' indices to the right, setting the left-most
/// `num' indices to the value `fill'.

View File

@ -451,6 +451,17 @@ namespace util {
}
template <
typename ValueT,
typename = std::enable_if_t<std::is_floating_point_v<ValueT>>
>
ValueT
frac (ValueT val)
{
return val - static_cast<long> (val);
}
///////////////////////////////////////////////////////////////////////////////
// angles, trig
namespace detail {