mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-08 23:19:40 +00:00
Remove: LeastCommonMultiple / GreatestCommonDivisor
Use std::lcm / std::gcd instead.
This commit is contained in:
parent
1403f24fa9
commit
4c51534b6a
@ -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.
|
||||
|
@ -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);
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user