Skip to content

Commit

Permalink
this commit resolves #4
Browse files Browse the repository at this point in the history
  • Loading branch information
devKathy committed Jun 24, 2023
1 parent 8e39884 commit 4e6821b
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Community Toolbox/Community Toolbox.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
function CommunityToolboxSuite() : VerrificSuiteGroup("Community toolbox tests") constructor {
add_suite(new MiscUtilsSuite());
add_suite(new MathUtilsSuite());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function MathUtilsSuite() : VerrificSuiteGroup("Math utilities tests") constructor {
add_methods_from(EucmoddivTests);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function EucmoddivTests(_run, _method) : VerrificMethodTest(_run, _method) constructor {
static test_subject = "eucmod/eucdiv";
static should_be_positive_eucmod_pos_pos = function() {
_a = 7;
_b = 3;
_r = eucmod(_a,_b);
assert_is_true( _r >= 0 && _r < abs(_b));
}

static should_be_positive_eucmod_pos_neg = function() {
_a = 7;
_b = -3;
_r = eucmod(_a,_b);
assert_is_true( _r >= 0 && _r < abs(_b));
}

static should_be_positive_eucmod_neg_pos = function() {
_a = -7;
_b = 3;
_r = eucmod(_a,_b);
assert_is_true( _r >= 0 && _r < abs(_b));
}

static should_be_positive_eucmod_neg_neg = function() {
_a = -7;
_b = -3;
_r = eucmod(_a,_b);
assert_is_true( _r >= 0 && _r < abs(_b));
}

static should_equal_dividend_pos_pos = function() {
_a = 7;
_b = 3;
_result = eucdiv(_a, _b) * _b + eucmod(_a, _b);
assert_is_true(_a == _result);
}

static should_equal_dividend_pos_neg = function() {
_a = 7;
_b = -3;
_result = eucdiv(_a, _b) * _b + eucmod(_a, _b);
assert_is_true(_a == _result);
}

static should_equal_dividend_neg_pos = function() {
_a = -7;
_b = 3;
_result = eucdiv(_a, _b) * _b + eucmod(_a, _b);
assert_is_true(_a == _result);
}

static should_equal_dividend_neg_neg = function() {
_a = -7;
_b = -3;
_result = eucdiv(_a, _b) * _b + eucmod(_a, _b);
assert_is_true(_a == _result);
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Community Toolbox/scripts/utils_Math/utils_Math.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// @func eucmod(dvnd,dvsr)
/// @desc Calculates a remainder from the Euclidian division (the remainder will always be non-negative).
/// @arg {Real} dvnd The value to divide, or dividend.
/// @arg {Real} dvsr The value to divide by, or divisor.
/// @returns {Real}
function eucmod(_dvnd,_dvsr){
_r = _dvnd%_dvsr;
if (_r > 0)
return _r;
else if (_dvsr > 0)
return _r + _dvsr;
else
return _r - _dvsr;
}

/// @func eucdiv(dvnd,dvsr)
/// @desc Calculates an integer quotient of the Euclidian division (i.e. with non-negative remainder).
/// @arg {Real} dvnd The value to divide, or dividend.
/// @arg {Real} dvsr The value to divide by, or divisor.
/// @returns {Real}
function eucdiv(_dvnd,_dvsr){
_q = _dvnd div _dvsr;

if (_dvnd > 0)
return _q;
else if (_dvsr > 0)
return _q - 1;
else
return _q + 1;
}
11 changes: 11 additions & 0 deletions Community Toolbox/scripts/utils_Math/utils_Math.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e6821b

Please sign in to comment.