From b2bcebf9126af524e8a8e44561750796d3e54cbf Mon Sep 17 00:00:00 2001 From: cyn0x8 Date: Wed, 22 Jan 2025 22:43:32 -0800 Subject: [PATCH 1/4] add mod function --- flixel/math/FlxMath.hx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/flixel/math/FlxMath.hx b/flixel/math/FlxMath.hx index f4f7f1ef49..a78a707a52 100644 --- a/flixel/math/FlxMath.hx +++ b/flixel/math/FlxMath.hx @@ -573,4 +573,13 @@ class FlxMath { return (n > 0) ? n : -n; } + + /** + * Returns `a mod b`. + */ + public static inline function mod(a:Float, b:Float):Float + { + b = Math.abs(b); + return a - b * Math.floor(a / b); + } } From 7d013446e14286e2b74a0f9dfa55ef0a908d1e58 Mon Sep 17 00:00:00 2001 From: cyn0x8 Date: Thu, 23 Jan 2025 12:12:45 -0800 Subject: [PATCH 2/4] add mod test --- tests/unit/src/flixel/math/FlxMathTest.hx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/src/flixel/math/FlxMathTest.hx b/tests/unit/src/flixel/math/FlxMathTest.hx index d6c1150feb..d464ae55e0 100644 --- a/tests/unit/src/flixel/math/FlxMathTest.hx +++ b/tests/unit/src/flixel/math/FlxMathTest.hx @@ -102,4 +102,16 @@ class FlxMathTest extends FlxTest // #2126 Assert.areEqual(1521730678.942, FlxMath.roundDecimal(1521730678.942, 3)); } + + @Test + function testMod() + { + Assert.areEqual(0, FlxMath.mod(8, 4)); + Assert.areEqual(3, FlxMath.mod(8, 5)); + Assert.areEqual(0.355, FlxMath.mod(0.941, 0.586)); + Assert.areEqual(1, FlxMath.mod(-5, 3)); + Assert.areEqual(1, FlxMath.mod(-5, -3)); + Assert.areEqual(0.128, FlxMath.mod(-0.834, 0.481)); + Assert.areEqual(2.5, FlxMath.mod(9, -6.5)); + } } From 8670754888ef2b4e6d3a33531ffb46da29e97622 Mon Sep 17 00:00:00 2001 From: cyn0x8 Date: Thu, 23 Jan 2025 12:17:08 -0800 Subject: [PATCH 3/4] update mod docs --- flixel/math/FlxMath.hx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/flixel/math/FlxMath.hx b/flixel/math/FlxMath.hx index a78a707a52..fddf1d4f67 100644 --- a/flixel/math/FlxMath.hx +++ b/flixel/math/FlxMath.hx @@ -575,7 +575,15 @@ class FlxMath } /** - * Returns `a mod b`. + * Performs a modulo operation on two numbers. + * This function differs from the built-in `%` operator in that it always + * returns a positive result even with negative numbers involved. + * For example, `-5 % 3` would return `-2` (truncated division), + * while `mod(-5, 3)` would return `1` (Euclidean division). + * + * @param a The dividend. + * @param b The divisor. + * @return `a mod b`. */ public static inline function mod(a:Float, b:Float):Float { From bbd210b0e3ae315bef117c265ddd14254d98d59c Mon Sep 17 00:00:00 2001 From: cyn0x8 Date: Mon, 27 Jan 2025 19:04:22 -0800 Subject: [PATCH 4/4] update mod docs 2 --- flixel/math/FlxMath.hx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/flixel/math/FlxMath.hx b/flixel/math/FlxMath.hx index fddf1d4f67..a2da6e90b0 100644 --- a/flixel/math/FlxMath.hx +++ b/flixel/math/FlxMath.hx @@ -575,11 +575,13 @@ class FlxMath } /** - * Performs a modulo operation on two numbers. - * This function differs from the built-in `%` operator in that it always - * returns a positive result even with negative numbers involved. - * For example, `-5 % 3` would return `-2` (truncated division), - * while `mod(-5, 3)` would return `1` (Euclidean division). + * Performs a modulo operation to calculate the remainder of `a` divided by `b`. + * + * The definition of "remainder" varies by implementation; + * this one is similar to GLSL or Python in that it uses Euclidean division, which always returns positive, + * while Haxe's `%` operator uses signed truncated division. + * + * For example, `-5 % 3` returns `-2` while `FlxMath.mod(-5, 3)` returns `1`. * * @param a The dividend. * @param b The divisor.