From da1260b0278b22e1cc6d48152dd2cce2b26cbfc8 Mon Sep 17 00:00:00 2001 From: Arnav495 Date: Sat, 14 Feb 2026 11:11:28 -0800 Subject: [PATCH] Brute force CRT. --- src/main/java/frc/robot/util/Remainders.java | 25 +++++++++++++++ .../java/frc/robot/util/RemainderTest.java | 31 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/frc/robot/util/Remainders.java create mode 100644 src/test/java/frc/robot/util/RemainderTest.java diff --git a/src/main/java/frc/robot/util/Remainders.java b/src/main/java/frc/robot/util/Remainders.java new file mode 100644 index 0000000..0086c8d --- /dev/null +++ b/src/main/java/frc/robot/util/Remainders.java @@ -0,0 +1,25 @@ +package frc.robot.util; + +public class Remainders { + public record Encoder(double val, double mod) { + } + + public static double compute(Encoder a, Encoder b, double tolerance) { + double testing = a.val(); + while (true) { + if (Math.abs(testing % a.mod() - a.val()) <= tolerance && + Math.abs(testing % b.mod() - b.val()) <= tolerance) + return testing; + + // alternately check positive and negative + if (testing > a.val()) { + double diff = testing - a.val(); + testing -= 2 * diff; + } else { + double diff = a.val() - testing; + testing += 2 * diff; + testing += a.mod(); + } + } + } +} diff --git a/src/test/java/frc/robot/util/RemainderTest.java b/src/test/java/frc/robot/util/RemainderTest.java new file mode 100644 index 0000000..52cbf01 --- /dev/null +++ b/src/test/java/frc/robot/util/RemainderTest.java @@ -0,0 +1,31 @@ + +package frc.robot.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import frc.robot.util.Remainders.Encoder; + +public class RemainderTest { + + @BeforeEach + public void prepare() { + } + + @AfterEach + public void cleanup() { + } + + @Test + public void test() { + double tolerance = 0.01; + + Encoder a = new Encoder(5000 % 123, 123); + Encoder b = new Encoder(5000 % 321, 321); + double val = Remainders.compute(a, b, tolerance); + assertEquals(5000, val, tolerance); + } +} -- 2.39.5