From 92d6193360f5db5e609c0a98a6e9f169181c12a6 Mon Sep 17 00:00:00 2001
From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com>
Date: Mon, 6 Nov 2023 15:29:50 -0600
Subject: [PATCH] Add modulo operation
---
src/Mathematics.NET/Core/IReal.cs | 1 +
.../Core/Operations/IModuloOperation.cs | 37 +++++++++++++++++++
src/Mathematics.NET/Core/Rational.cs | 11 ++++++
src/Mathematics.NET/Core/Real.cs | 1 +
4 files changed, 50 insertions(+)
create mode 100644 src/Mathematics.NET/Core/Operations/IModuloOperation.cs
diff --git a/src/Mathematics.NET/Core/IReal.cs b/src/Mathematics.NET/Core/IReal.cs
index e48ec194..3b9a1bbb 100644
--- a/src/Mathematics.NET/Core/IReal.cs
+++ b/src/Mathematics.NET/Core/IReal.cs
@@ -35,6 +35,7 @@ namespace Mathematics.NET.Core;
/// A type that implements the interface
public interface IReal
: IComplex,
+ IModuloOperation,
IInequalityRelations,
IDecrementOperation,
IIncrementOperation,
diff --git a/src/Mathematics.NET/Core/Operations/IModuloOperation.cs b/src/Mathematics.NET/Core/Operations/IModuloOperation.cs
new file mode 100644
index 00000000..bc11ec15
--- /dev/null
+++ b/src/Mathematics.NET/Core/Operations/IModuloOperation.cs
@@ -0,0 +1,37 @@
+//
+// Mathematics.NET
+// https://github.com/HamletTanyavong/Mathematics.NET
+//
+// MIT License
+//
+// Copyright (c) 2023 Hamlet Tanyavong
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+//
+
+namespace Mathematics.NET.Core.Operations;
+
+/// Defines a mechanism for computing modulos
+/// The input type
+/// The output type
+public interface IModuloOperation
+ where TInput : IModuloOperation
+{
+ static abstract TOutput operator %(TInput left, TInput right);
+}
diff --git a/src/Mathematics.NET/Core/Rational.cs b/src/Mathematics.NET/Core/Rational.cs
index 2250c7e0..ebe22264 100644
--- a/src/Mathematics.NET/Core/Rational.cs
+++ b/src/Mathematics.NET/Core/Rational.cs
@@ -151,6 +151,17 @@ public Rational(T p, T q)
return new(num / gcd, den / gcd);
}
+ public static Rational operator %(Rational x, Rational y)
+ {
+ if (y._denominator == T.Zero)
+ {
+ return NaN;
+ }
+
+ var q = x / y;
+ return T.DivRem(q._numerator, q._denominator).Remainder;
+ }
+
//
// Equality
//
diff --git a/src/Mathematics.NET/Core/Real.cs b/src/Mathematics.NET/Core/Real.cs
index 2ec49d84..8865dfbe 100644
--- a/src/Mathematics.NET/Core/Real.cs
+++ b/src/Mathematics.NET/Core/Real.cs
@@ -148,6 +148,7 @@ public Real(double real)
public static Real operator -(Real left, Real right) => left._value - right._value;
public static Real operator *(Real left, Real right) => left._value * right._value;
public static Real operator /(Real left, Real right) => left._value / right._value;
+ public static Real operator %(Real left, Real right) => left._value % right._value;
//
// Equality