Add floating specialisations for range::random

This commit is contained in:
Danny Robson 2012-01-04 17:05:09 +11:00
parent f499779ffc
commit af97b70377

View File

@ -60,6 +60,17 @@ range<T>::sanity (void) const
{ check (min <= max); }
namespace util {
template <>
void
range<double>::sanity (void) const {
if (std::isnan (min) || std::isnan (max))
return;
check (min <= max);
}
}
template <typename T>
T
range<T>::clamp (T val) const
@ -74,11 +85,26 @@ range<T>::normalise (T val) const {
}
namespace util {
template <>
double
range<double>::random (void) const {
double pos = ::rand () / (double)(RAND_MAX);
return (max - min) * pos + min;
}
template <>
float
range<float>::random (void) const {
float pos = ::rand () / (float)(RAND_MAX);
return (max - min) * pos + min;
}
}
template <typename T>
T
range<T>::random (void) const {
double pos = ::rand () / (double)(RAND_MAX);
return (max - min) * pos + min;
return min + (T)::rand () % (max - min);
}