Rational 2.0.0

dotnet add package Rational --version 2.0.0
                    
NuGet\Install-Package Rational -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Rational" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rational" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Rational" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Rational --version 2.0.0
                    
#r "nuget: Rational, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Rational@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Rational&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Rational&version=2.0.0
                    
Install as a Cake Tool

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.Zero
  • Rational.One
  • Rational.Half
  • Rational.NaN
  • Rational.PositiveInfinity
  • Rational.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 form n/d
  • D / 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.Abs
  • Rational.Truncate
  • Rational.Floor
  • Rational.Ceiling
  • Rational.Round / Rational.Round(..., MidpointRounding)
  • Rational.Pow
  • Rational.Log
  • Rational.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 87 2/25/2026
1.1.0 391 6/14/2023
1.0.2 593 5/17/2022
1.0.1 560 5/17/2022
1.0.0 542 5/17/2022