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:
parent
7f00f92e59
commit
cdac2dcbc2
@ -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'.
|
||||
|
11
maths.hpp
11
maths.hpp
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user