mirror of
https://github.com/jslightham/cpp-utils.git
synced 2026-03-09 18:12:26 +01:00
20 lines
469 B
C++
20 lines
469 B
C++
|
|
#ifndef NUMBER_UTILS_H
|
|
#define NUMBER_UTILS_H
|
|
|
|
#include <iostream>
|
|
#include <type_traits>
|
|
|
|
// Map a value with range, to another range.
|
|
template<typename T>
|
|
T mapRange(T value, T inMin, T inMax, T outMin, T outMax) {
|
|
static_assert(std::is_arithmetic<T>::value, "Template parameter must be a numeric type");
|
|
if (inMin == inMax) {
|
|
return value;
|
|
}
|
|
|
|
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
|
|
}
|
|
|
|
#endif //NUMBER_UTILS_H
|