mirror of
https://github.com/TomHarte/CLK.git
synced 2026-04-22 08:16:42 +00:00
Made an attempt to insert proper conversions to/from rotational speed. The Time class is now really turning into a full-on quotient. Might need refactoring.
This commit is contained in:
+44
-5
@@ -25,11 +25,12 @@ struct Time {
|
||||
Reduces this @c Time to its simplest form — eliminates all common factors from @c length
|
||||
and @c clock_rate.
|
||||
*/
|
||||
inline void simplify()
|
||||
inline Time &simplify()
|
||||
{
|
||||
unsigned int common_divisor = NumberTheory::greatest_common_divisor(length, clock_rate);
|
||||
length /= common_divisor;
|
||||
clock_rate /= common_divisor;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -40,12 +41,12 @@ struct Time {
|
||||
return (float)length / (float)clock_rate;
|
||||
}
|
||||
|
||||
inline bool operator<(Time other)
|
||||
inline bool operator < (Time other)
|
||||
{
|
||||
return other.clock_rate * length < clock_rate * other.length;
|
||||
}
|
||||
|
||||
inline Time operator+(Time other)
|
||||
inline Time operator + (Time other)
|
||||
{
|
||||
Time result;
|
||||
result.clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
||||
@@ -53,7 +54,7 @@ struct Time {
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Time &operator +=(Time other)
|
||||
inline Time &operator += (Time other)
|
||||
{
|
||||
unsigned int combined_clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
||||
length = length * (combined_clock_rate / clock_rate) + other.length * (combined_clock_rate / other.clock_rate);
|
||||
@@ -61,7 +62,7 @@ struct Time {
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Time operator-(Time other)
|
||||
inline Time operator - (Time other)
|
||||
{
|
||||
Time result;
|
||||
result.clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
||||
@@ -69,6 +70,44 @@ struct Time {
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Time operator -= (Time other)
|
||||
{
|
||||
unsigned int combined_clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
||||
length = length * (combined_clock_rate / clock_rate) - other.length * (combined_clock_rate / other.clock_rate);
|
||||
clock_rate = combined_clock_rate;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Time operator * (Time other)
|
||||
{
|
||||
Time result;
|
||||
result.clock_rate = clock_rate * other.clock_rate;
|
||||
result.length = length * other.length;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Time &operator *= (Time other)
|
||||
{
|
||||
length *= other.length;
|
||||
clock_rate *= other.clock_rate;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Time operator / (Time other)
|
||||
{
|
||||
Time result;
|
||||
result.clock_rate = clock_rate * other.length;
|
||||
result.length = length * other.clock_rate;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Time &operator /= (Time other)
|
||||
{
|
||||
length *= other.clock_rate;
|
||||
clock_rate *= other.length;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline void set_zero()
|
||||
{
|
||||
length = 0;
|
||||
|
||||
Reference in New Issue
Block a user