1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-21 17:16:44 +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:
Thomas Harte
2016-08-04 21:36:39 -04:00
parent e57ab1025d
commit 3e90b85ff8
4 changed files with 59 additions and 17 deletions
+44 -5
View File
@@ -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;