Changeset - r28523:cdd8c63fd245
[Not reviewed]
master
0 3 0
Rubidium - 11 months ago 2024-01-20 14:59:53
rubidium@openttd.org
Remove: LeastCommonMultiple / GreatestCommonDivisor

Use std::lcm / std::gcd instead.
3 files changed with 0 insertions and 72 deletions:
0 comments (0 inline, 0 general)
src/core/math_func.cpp
Show inline comments
 
@@ -13,41 +13,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.
 
 * @param a Dividend.
src/core/math_func.hpp
Show inline comments
 
@@ -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);
 

	
 
/**
src/tests/math_func.cpp
Show inline comments
 
@@ -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));
0 comments (0 inline, 0 general)