Merge pull request #320 from CesiumGS/math-roundupdown
This commit is contained in:
commit
569792cc5a
|
|
@ -9,6 +9,7 @@
|
|||
##### Additions :tada:
|
||||
|
||||
- Added `Future<T>::share`, which returns a `SharedFuture<T>` and allows multiple continuations to be attached.
|
||||
- Added `Math::roundUp` and `Math::roundDown`.
|
||||
- Added `Rectangle::computeUnion`.
|
||||
|
||||
### v0.6.0 - 2021-08-02
|
||||
|
|
|
|||
|
|
@ -395,6 +395,46 @@ public:
|
|||
|
||||
return simplified;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Rounds a value up to the nearest integer, like `ceil`, except
|
||||
* that if the value is very close to the lower integer it is rounded down
|
||||
* (like `floor`) instead.
|
||||
*
|
||||
* @param value The value to round.
|
||||
* @param tolerance The tolerance. If the value is closer than this to the
|
||||
* lower integer, it is rounded down instead.
|
||||
* @return The rounded value.
|
||||
*/
|
||||
static double roundUp(double value, double tolerance) {
|
||||
double up = glm::ceil(value);
|
||||
double down = glm::floor(value);
|
||||
if (value - down < tolerance) {
|
||||
return down;
|
||||
} else {
|
||||
return up;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Rounds a value down to the nearest integer, like `floor`, except
|
||||
* that if the value is very close to the higher integer it is rounded up
|
||||
* (like `ceil`) instead.
|
||||
*
|
||||
* @param value The value to round.
|
||||
* @param tolerance The tolerance. If the value is closer than this to the
|
||||
* higher integer, it is rounded up instead.
|
||||
* @return The rounded value.
|
||||
*/
|
||||
static double roundDown(double value, double tolerance) {
|
||||
double up = glm::ceil(value);
|
||||
double down = glm::floor(value);
|
||||
if (up - value < tolerance) {
|
||||
return up;
|
||||
} else {
|
||||
return down;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace CesiumUtility
|
||||
|
|
|
|||
|
|
@ -50,3 +50,23 @@ TEST_CASE("Math::convertLongitudeRange example") {
|
|||
//! [convertLongitudeRange]
|
||||
CHECK(longitude == CesiumUtility::Math::degreesToRadians(-90.0));
|
||||
}
|
||||
|
||||
TEST_CASE("Math::roundUp and roundDown") {
|
||||
CHECK(Math::roundUp(1.0, 0.01) == 1.0);
|
||||
CHECK(Math::roundDown(1.0, 0.01) == 1.0);
|
||||
|
||||
CHECK(Math::roundUp(1.01, 0.01) == 2.0);
|
||||
CHECK(Math::roundDown(1.99, 0.01) == 1.0);
|
||||
|
||||
CHECK(Math::roundUp(1.005, 0.01) == 1.0);
|
||||
CHECK(Math::roundDown(1.995, 0.01) == 2.0);
|
||||
|
||||
CHECK(Math::roundUp(-1.0, 0.01) == -1.0);
|
||||
CHECK(Math::roundDown(-1.0, 0.01) == -1.0);
|
||||
|
||||
CHECK(Math::roundUp(-1.99, 0.01) == -1.0);
|
||||
CHECK(Math::roundDown(-1.01, 0.01) == -2.0);
|
||||
|
||||
CHECK(Math::roundUp(-1.995, 0.01) == -2.0);
|
||||
CHECK(Math::roundDown(-1.005, 0.01) == -1.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue