Skip to content

Commit

Permalink
Add floor and ceiling functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HamletTanyavong committed Nov 6, 2023
1 parent 8cec2c5 commit dc6e605
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Mathematics.NET/Core/IReal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public interface IReal<T>
/// <summary>The backing value of the type</summary>
double Value { get; }

/// <summary>Compute the ceiling function of a value</summary>
/// <param name="x">A value</param>
/// <returns>The smallest integer greater than or equal to the value</returns>
static abstract T Ceiling(T x);

/// <summary>Compute the floor function of a value</summary>
/// <param name="x">A value</param>
/// <returns>The largest integer less than or equal to the value</returns>
static abstract T Floor(T x);

/// <summary>Check if a value is negative infinity</summary>
/// <param name="x">The value to check</param>
/// <returns><c>true</c> if the value is negative infinity; otherwise, <c>false</c></returns>
Expand Down
8 changes: 8 additions & 0 deletions src/Mathematics.NET/Core/Rational.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,16 @@ public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatPro

public static Rational<T> Abs(Rational<T> x) => new(T.Abs(x._numerator), T.Abs(x._denominator));

public static Rational<T> Ceiling(Rational<T> x)
{
var (quotient, remainder) = T.DivRem(x._numerator, x._denominator);
return remainder == T.Zero ? quotient : quotient + T.One;
}

public static Rational<T> Conjugate(Rational<T> x) => x;

public static Rational<T> Floor(Rational<T> x) => T.DivRem(x._numerator, x._denominator).Quotient;

public static Rational<T> FromDouble(double x) => (Rational<T>)x;

public static Rational<T> FromReal(Real x) => (Rational<T>)x;
Expand Down
4 changes: 4 additions & 0 deletions src/Mathematics.NET/Core/Real.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,12 @@ public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatPro

public static Real Abs(Real x) => Math.Abs(x._value);

public static Real Ceiling(Real x) => Math.Ceiling(x._value);

public static Real Conjugate(Real x) => x;

public static Real Floor(Real x) => Math.Floor(x._value);

public static Real FromDouble(double x) => new(x);

public static Real FromReal(Real x) => x;
Expand Down

0 comments on commit dc6e605

Please sign in to comment.