range: templatise normalise methods return type

This commit is contained in:
Danny Robson 2015-02-17 16:18:55 +11:00
parent 1c7a92ee48
commit 9d6be23dd5
4 changed files with 47 additions and 10 deletions

View File

@ -135,6 +135,7 @@ UTIL_FILES = \
rational.hpp \
region.cpp \
region.hpp \
region.ipp \
si.cpp \
signal.cpp \
signal.hpp \

View File

@ -87,14 +87,6 @@ range<T>::expand (T val) {
}
template <typename T>
double
range<T>::normalise (T val) const {
return ((double)val - min) /
((double)max - min);
}
template <typename T>
range<T>&
range<T>::operator*= (T val) {

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
*/
@ -52,7 +52,8 @@ namespace util {
/// Normalise a number to [0, 1] within the range. Does not check
/// bounds, it is the caller's responsibility to clamp the result if
/// needed.
double normalise (T val) const;
template <typename U>
U normalise (T val) const;
range& operator*= (T);
range operator* (T) const;
@ -92,4 +93,6 @@ namespace util {
}
}
#include "range.ipp"
#endif

41
range.ipp Normal file
View File

@ -0,0 +1,41 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_RANGE_IPP
#error
#else
#define __UTIL_RANGE_IPP
#endif
#include <type_traits>
//-----------------------------------------------------------------------------
namespace util {
template <typename T>
template <typename U>
U
range<T>::normalise (T val) const {
static_assert (std::is_floating_point<U>::value,
"normalise isn't implemented for integer types");
return static_cast<U> (val - min) /
static_cast<U> (max - min);
}
}