Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve range reduction algorithm #169

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions src/OpenSpaceToolkit/Physics/Units/Derived/Angle.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Apache License 2.0
#include <cmath>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply for consistency:

Suggested change
#include <cmath>
#include <cmath>


#include <OpenSpaceToolkit/Core/Error.hpp>
#include <OpenSpaceToolkit/Core/Utilities.hpp>
Expand Down Expand Up @@ -61,7 +62,7 @@ bool Angle::operator==(const Angle& anAngle) const
{
return false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

switch (unit_)
{
case Angle::Unit::Radian:
Expand Down Expand Up @@ -356,7 +357,7 @@ Real Angle::in(const Angle::Unit& aUnit) const
return this->accessValue();
}

return this->accessValue() * Angle::SIRatio(unit_) / Angle::SIRatio(aUnit);
return (this->accessValue() * Angle::SIRatio(unit_) / Angle::SIRatio(aUnit));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (this->accessValue() * Angle::SIRatio(unit_) / Angle::SIRatio(aUnit));
return this->accessValue() * Angle::SIRatio(unit_) / Angle::SIRatio(aUnit);

}

Real Angle::inRadians() const
Expand Down Expand Up @@ -732,24 +733,20 @@ Real Angle::ReduceRange(const Real& aValue, const Real& aRangeLowerBound, const
"Lower bound [{}] greater than or equal to upper bound [{}].", aRangeLowerBound, aRangeUpperBound
);
}
// this line handles some rounding error albeit in a way that doesnt pass the smell test...
Real value = aValue + 10.0 - 10.0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Real value = aValue + 10.0 - 10.0;
const Real value = aValue + 10.0 - 10.0;

const Real excessValue = std::fmod(value - aRangeLowerBound, aRangeUpperBound - aRangeLowerBound);
const Real adjustedRangeValue = aRangeLowerBound + excessValue;

Real value = aValue;

const Real range = aRangeUpperBound - aRangeLowerBound;

while (value < aRangeLowerBound
) // [TBM] This is a STUPID implementation: just used as a logic placeholder... should be improved ASAP
if ((adjustedRangeValue >= aRangeLowerBound) && (adjustedRangeValue < aRangeUpperBound))
{
value += range;
return adjustedRangeValue;
}

while (value >= aRangeUpperBound
) // [TBM] This is a STUPID implementation: just used as a logic placeholder... should be improved ASAP
{
value -= range;
}

return value;
// Addition here might seem unintuitive, but this edge case is specifically
// to capture when excessValue is a different sign than the range (often
// a negative value in a positive range) and we are actually adding a
// negative number to the upper bound.
return aRangeUpperBound + excessValue;
}

} // namespace units
Expand Down