mirror of
https://github.com/jslightham/cpp-utils.git
synced 2026-03-09 18:12:26 +01:00
Initial commit
This commit is contained in:
19
Number.h
Normal file
19
Number.h
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
#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
|
||||
Reference in New Issue
Block a user