Skip to content

Commit

Permalink
Add modulo operation
Browse files Browse the repository at this point in the history
  • Loading branch information
HamletTanyavong committed Nov 6, 2023
1 parent dc6e605 commit 92d6193
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Mathematics.NET/Core/IReal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Mathematics.NET.Core;
/// <typeparam name="T">A type that implements the interface</typeparam>
public interface IReal<T>
: IComplex<T>,
IModuloOperation<T, T>,
IInequalityRelations<T, bool>,
IDecrementOperation<T>,
IIncrementOperation<T>,
Expand Down
37 changes: 37 additions & 0 deletions src/Mathematics.NET/Core/Operations/IModuloOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <copyright file="IModuloOperation.cs" company="Mathematics.NET">
// 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.
// </copyright>

namespace Mathematics.NET.Core.Operations;

/// <summary>Defines a mechanism for computing modulos</summary>
/// <typeparam name="TInput">The input type</typeparam>
/// <typeparam name="TOutput">The output type</typeparam>
public interface IModuloOperation<TInput, TOutput>
where TInput : IModuloOperation<TInput, TOutput>
{
static abstract TOutput operator %(TInput left, TInput right);
}
11 changes: 11 additions & 0 deletions src/Mathematics.NET/Core/Rational.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ public Rational(T p, T q)
return new(num / gcd, den / gcd);
}

public static Rational<T> operator %(Rational<T> x, Rational<T> y)
{
if (y._denominator == T.Zero)
{
return NaN;
}

var q = x / y;
return T.DivRem(q._numerator, q._denominator).Remainder;
}

//
// Equality
//
Expand Down
1 change: 1 addition & 0 deletions src/Mathematics.NET/Core/Real.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 92d6193

Please sign in to comment.