Skip to content

Commit

Permalink
Restrict valid types for periodic fold
Browse files Browse the repository at this point in the history
  • Loading branch information
RudolfWeeber committed Jul 16, 2024
1 parent fe94b6a commit 0736bd9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core/algorithm/periodic_fold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
#define CORE_ALGORITHM_PERIODIC_FOLD_HPP

#include <cmath>
#include <concepts>
#include <limits>
#include <type_traits>
#include <utility>

// Define a concept that checks if a type T is an integer or a reference to an
// integer
template <typename T>
concept IntegralOrRef = std::integral<std::remove_reference_t<T>>;
template <typename T>
concept FloatingPointOrRef = std::floating_point<std::remove_reference_t<T>>;

namespace Algorithm {
/**
* @brief Fold value into primary interval.
Expand All @@ -32,7 +41,7 @@ namespace Algorithm {
* @param l Length of primary interval
* @return x folded into [0, l) and number of folds.
*/
template <typename T, typename I>
template <FloatingPointOrRef T, IntegralOrRef I>
std::pair<T, I> periodic_fold(T x, I i, T const l) {
using limits = std::numeric_limits<I>;

Expand All @@ -56,7 +65,7 @@ std::pair<T, I> periodic_fold(T x, I i, T const l) {
* @param l Length of primary interval
* @return x folded into [0, l).
*/
template <typename T> T periodic_fold(T x, T const l) {
template <FloatingPointOrRef T> T periodic_fold(T x, T const l) {
#ifndef __FAST_MATH__
/* Can't fold if either x or l is nan or inf. */
if (std::isnan(x) or std::isnan(l) or std::isinf(x) or (l == 0)) {
Expand Down

0 comments on commit 0736bd9

Please sign in to comment.