Rational 2.0.0
dotnet add package Rational --version 2.0.0
NuGet\Install-Package Rational -Version 2.0.0
<PackageReference Include="Rational" Version="2.0.0" />
<PackageVersion Include="Rational" Version="2.0.0" />
<PackageReference Include="Rational" />
paket add Rational --version 2.0.0
#r "nuget: Rational, 2.0.0"
#:package Rational@2.0.0
#addin nuget:?package=Rational&version=2.0.0
#tool nuget:?package=Rational&version=2.0.0
RationalSharp
RationalSharp is a .NET library for exact rational number arithmetic in C#.
It provides a Rational type (ShInUeXx.Numerics.Rational) backed by BigInteger, with support for:
- exact fraction arithmetic (
1/3 + 1/6 == 1/2) - parsing and formatting (
"3 1/2","1.25e2", localized decimal separator) - special values (
NaN,+Infinity,-Infinity) - .NET generic math (
INumber<Rational>)
Requirements
- .NET
10.0(net10.0)
Installation
dotnet add package Rational
Quick Start
using ShInUeXx.Numerics;
var a = new Rational(1, 3);
var b = new Rational(1, 6);
var sum = a + b; // 1/2
Console.WriteLine(sum); // "1/2"
Console.WriteLine(sum == Rational.Half); // True
Creating Values
using ShInUeXx.Numerics;
using System.Numerics;
var x = new Rational(7, 5);
var y = new Rational(new BigInteger(42));
var fromDouble = Rational.From(0.125); // exact IEEE -> rational conversion
var fromDecimal = Rational.From(12.34m); // exact decimal -> rational conversion
Available predefined values include:
Rational.ZeroRational.OneRational.HalfRational.NaNRational.PositiveInfinityRational.NegativeInfinity
Parsing
Supported formats include:
- integer:
"123" - fraction:
"2/3" - mixed fraction:
"3 1/2" - decimal / scientific:
"1.25","1.25e2"
using ShInUeXx.Numerics;
using System.Globalization;
var a = Rational.Parse("2/3");
var b = Rational.Parse("3 1/2");
var c = Rational.Parse("1.25e2"); // 125
var pl = CultureInfo.GetCultureInfo("pl-PL");
var d = Rational.Parse("1,5", NumberStyles.AllowDecimalPoint, pl); // 3/2
Formatting
Rational supports ToString(format, provider) and TryFormat(...).
Format specifiers:
F(or empty): fraction formn/dD/D{precision}: decimal form (D2→ 2 fractional digits)W: whole + fraction form (1 1/2)
using ShInUeXx.Numerics;
using System.Globalization;
var x = new Rational(-1, 2);
Console.WriteLine(x.ToString("F", CultureInfo.InvariantCulture)); // -1/2
Console.WriteLine(x.ToString("D2", CultureInfo.InvariantCulture)); // -0.50
Console.WriteLine(x.ToString("W", CultureInfo.InvariantCulture)); // -0 1/2
Math Helpers
The library includes helpers such as:
Rational.AbsRational.TruncateRational.FloorRational.CeilingRational.Round/Rational.Round(..., MidpointRounding)Rational.PowRational.LogRational.Modulo(same semantics as%, truncate-toward-zero quotient)Rational.ModuloEuclidean(Euclidean modulo)
var r = new Rational(-1, 2);
var trunc = Rational.Truncate(r); // 0
var floor = Rational.Floor(r); // -1
var rem = new Rational(-1, 2) % new Rational(1, 3); // -1/6
var euclid = Rational.ModuloEuclidean(new Rational(-1, 2), new Rational(1, 3)); // 1/6
Continued Fractions
var r = Rational.FromContinuedForm([1, 2, 2]); // 7/5
var terms = r.ToContinuedForm<int>().ToArray(); // [1, 2, 2]
Notes:
FromContinuedForm(...)requires at least one term.ToContinuedForm(...)is defined for finite values only.
Special Values
Rational supports non-finite values using the internal representations:
NaN=0/0+Infinity=1/0-Infinity=-1/0
These values are propagated consistently across parsing/conversion/formatting where applicable.
Generic Math
Rational implements INumber<Rational>, so it can be used in generic numeric code in modern .NET.
static T AddOne<T>(T value) where T : INumber<T>
=> value + T.One;
var r = AddOne(new Rational(2, 3)); // 5/3
License
MIT (see LICENSE)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- JetBrains.Annotations (>= 2025.2.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.