Fix potential integer underflow in rounded up divisions

A new `Math::division_round_up()` function was added, allowing for easy
and correct computation of integer divisions when the result needs to
be rounded up.

Fixes #80358.

Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
This commit is contained in:
EddieBreeg 2023-08-07 20:19:20 +02:00 committed by Rémi Verschelde
parent 13a0d6e9b2
commit 8747c67d9e
No known key found for this signature in database
GPG key ID: C3336907360768E1
14 changed files with 81 additions and 41 deletions

View file

@ -110,6 +110,29 @@ TEST_CASE_TEMPLATE("[Math] round/floor/ceil", T, float, double) {
CHECK(Math::ceil((T)-1.9) == (T)-1.0);
}
TEST_CASE_TEMPLATE("[Math] integer division round up unsigned", T, uint32_t, uint64_t) {
CHECK(Math::division_round_up((T)0, (T)64) == 0);
CHECK(Math::division_round_up((T)1, (T)64) == 1);
CHECK(Math::division_round_up((T)63, (T)64) == 1);
CHECK(Math::division_round_up((T)64, (T)64) == 1);
CHECK(Math::division_round_up((T)65, (T)64) == 2);
CHECK(Math::division_round_up((T)65, (T)1) == 65);
}
TEST_CASE_TEMPLATE("[Math] integer division round up signed", T, int32_t, int64_t) {
CHECK(Math::division_round_up((T)0, (T)64) == 0);
CHECK(Math::division_round_up((T)1, (T)64) == 1);
CHECK(Math::division_round_up((T)63, (T)64) == 1);
CHECK(Math::division_round_up((T)64, (T)64) == 1);
CHECK(Math::division_round_up((T)65, (T)64) == 2);
CHECK(Math::division_round_up((T)65, (T)1) == 65);
CHECK(Math::division_round_up((T)-1, (T)64) == 0);
CHECK(Math::division_round_up((T)-1, (T)-1) == 1);
CHECK(Math::division_round_up((T)-1, (T)1) == -1);
CHECK(Math::division_round_up((T)-1, (T)-2) == 1);
CHECK(Math::division_round_up((T)-4, (T)-2) == 2);
}
TEST_CASE_TEMPLATE("[Math] sin/cos/tan", T, float, double) {
CHECK(Math::sin((T)-0.1) == doctest::Approx((T)-0.0998334166));
CHECK(Math::sin((T)0.1) == doctest::Approx((T)0.0998334166));