From 4c51534b6a19f5707e577c3e09a93bf44dfd2aa9 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 20 Jan 2024 15:59:53 +0100 Subject: [PATCH] Remove: LeastCommonMultiple / GreatestCommonDivisor Use std::lcm / std::gcd instead. --- src/core/math_func.cpp | 35 ----------------------------------- src/core/math_func.hpp | 2 -- src/tests/math_func.cpp | 35 ----------------------------------- 3 files changed, 72 deletions(-) diff --git a/src/core/math_func.cpp b/src/core/math_func.cpp index db705092a8..f094e9ece8 100644 --- a/src/core/math_func.cpp +++ b/src/core/math_func.cpp @@ -12,41 +12,6 @@ #include "../safeguards.h" -/** - * Compute least common multiple (lcm) of arguments \a a and \a b, the smallest - * integer value that is a multiple of both \a a and \a b. - * @param a First number. - * @param b second number. - * @return Least common multiple of values \a a and \a b. - * - * @note This function only works for non-negative values of \a a and \a b. - */ -int LeastCommonMultiple(int a, int b) -{ - if (a == 0 || b == 0) return 0; // By definition. - if (a == 1 || a == b) return b; - if (b == 1) return a; - - return a * b / GreatestCommonDivisor(a, b); -} - -/** - * Compute greatest common divisor (gcd) of \a a and \a b. - * @param a First number. - * @param b second number. - * @return Greatest common divisor of \a a and \a b. - */ -int GreatestCommonDivisor(int a, int b) -{ - while (b != 0) { - int t = b; - b = a % b; - a = t; - } - return a; - -} - /** * Deterministic approximate division. * Cancels out division errors stemming from the integer nature of the division over multiple runs. diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index 6cf0567015..561f8fb754 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -309,8 +309,6 @@ inline uint ToPercent16(uint i) return i * 101 >> 16; } -int LeastCommonMultiple(int a, int b); -int GreatestCommonDivisor(int a, int b); int DivideApprox(int a, int b); /** diff --git a/src/tests/math_func.cpp b/src/tests/math_func.cpp index 769e344145..1f53d005b7 100644 --- a/src/tests/math_func.cpp +++ b/src/tests/math_func.cpp @@ -13,41 +13,6 @@ #include "../core/math_func.hpp" -TEST_CASE("LeastCommonMultipleTest - Zero") -{ - CHECK(0 == LeastCommonMultiple(0, 0)); - CHECK(0 == LeastCommonMultiple(0, 600)); - CHECK(0 == LeastCommonMultiple(600, 0)); -} - -TEST_CASE("LeastCommonMultipleTest - FindLCM") -{ - CHECK(25 == LeastCommonMultiple(5, 25)); - CHECK(25 == LeastCommonMultiple(25, 5)); - CHECK(130 == LeastCommonMultiple(5, 26)); - CHECK(130 == LeastCommonMultiple(26, 5)); -} - -TEST_CASE("GreatestCommonDivisorTest - Negative") -{ - CHECK(4 == GreatestCommonDivisor(4, -52)); - // CHECK(3 == GreatestCommonDivisor(-27, 6)); // error - returns -3 -} - -TEST_CASE("GreatestCommonDivisorTest - Zero") -{ - CHECK(27 == GreatestCommonDivisor(0, 27)); - CHECK(27 == GreatestCommonDivisor(27, 0)); -} - -TEST_CASE("GreatestCommonDivisorTest - FindGCD") -{ - CHECK(5 == GreatestCommonDivisor(5, 25)); - CHECK(5 == GreatestCommonDivisor(25, 5)); - CHECK(1 == GreatestCommonDivisor(7, 27)); - CHECK(1 == GreatestCommonDivisor(27, 7)); -} - TEST_CASE("DivideApproxTest - Negative") { CHECK(-2 == DivideApprox(-5, 2));