From da4a0b882685b2fcfe53f0e6de8cbf53dfe66515 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 15 Jun 2024 15:55:24 +0800 Subject: [PATCH] bahaghari/lib: fix math.mod' implementation bug --- subprojects/bahaghari/lib/math.nix | 15 +++++++-------- subprojects/bahaghari/tests/lib/math.nix | 5 +++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/subprojects/bahaghari/lib/math.nix b/subprojects/bahaghari/lib/math.nix index 15dd7c6b..f527e4ec 100644 --- a/subprojects/bahaghari/lib/math.nix +++ b/subprojects/bahaghari/lib/math.nix @@ -278,17 +278,16 @@ rec { let base' = abs base; number' = abs number; - difference = number' * ((floor base' / (floor number')) + 1); + difference = number' * ((floor (base' / number')) + 1); - result = number' - (difference - base'); + result = abs (number' - (difference - base')); in - if number' > base' then - base + if number' > base' + then base else - if base < 0 then - -(result) - else - result; + if base < 0 + then -(result) + else result; /* Adds all of the given items on the list starting from a sum of zero. diff --git a/subprojects/bahaghari/tests/lib/math.nix b/subprojects/bahaghari/tests/lib/math.nix index 204ef06e..20de1aeb 100644 --- a/subprojects/bahaghari/tests/lib/math.nix +++ b/subprojects/bahaghari/tests/lib/math.nix @@ -262,6 +262,11 @@ lib.runTests { expected = 39194.200000000186; }; + testMathMod8 = { + expr = self.math.mod' 567.5 3.5; + expected = 0.5; + }; + testMathModBothPositive = { expr = self.math.mod' 54.5 20.5; expected = 13.5;