mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-04 02:41:19 +00:00
25ead80d8b
Round result of std::fmod(hours, 24) to the nearest float below 24 on double to float conversion when it is not. Add special type and conversion function along with tests to be used in all places where such conversion happens. To avoid producing hours equal to 24 due to double to float precision loss.
40 lines
869 B
C++
40 lines
869 B
C++
#ifndef GAME_MWWORLD_DURATION_H
|
|
#define GAME_MWWORLD_DURATION_H
|
|
|
|
#include <cmath>
|
|
#include <stdexcept>
|
|
|
|
namespace MWWorld
|
|
{
|
|
inline const double maxFloatHour = static_cast<double>(std::nextafter(24.0f, 0.0f));
|
|
|
|
class Duration
|
|
{
|
|
public:
|
|
static Duration fromHours(double hours)
|
|
{
|
|
if (hours < 0)
|
|
throw std::runtime_error("Negative hours is not supported Duration");
|
|
|
|
return Duration(
|
|
static_cast<int>(hours / 24), static_cast<float>(std::min(std::fmod(hours, 24), maxFloatHour)));
|
|
}
|
|
|
|
int getDays() const { return mDays; }
|
|
|
|
float getHours() const { return mHours; }
|
|
|
|
private:
|
|
int mDays;
|
|
float mHours;
|
|
|
|
explicit Duration(int days, float hours)
|
|
: mDays(days)
|
|
, mHours(hours)
|
|
{
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif
|