Rationalz 1.0.0
dotnet add package Rationalz --version 1.0.0
NuGet\Install-Package Rationalz -Version 1.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="Rationalz" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rationalz" Version="1.0.0" />
<PackageReference Include="Rationalz" />
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 Rationalz --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Rationalz, 1.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 Rationalz@1.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=Rationalz&version=1.0.0
#tool nuget:?package=Rationalz&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Rationalz
Rationalz - библиотека для работы с рациональными числами в виде несократимой дроби Numerator / Denominator.
Основной тип библиотеки - Rational<T>, где T должен быть целочисленным типом, реализующим IBinaryInteger<T> (int, long, BigInteger и т.д.).
Возможности
- автоматическая нормализация дроби и сокращение по НОД;
- хранение знаменателя в положительном виде;
- арифметические операции
+,-,*,/,%, unary+, unary-,++,--; - сравнение
==,!=,<,<=,>,>=; - парсинг из строк формата
"3/4"и"42"; - форматирование через
ToString(...)иTryFormat(...); - преобразования в
double,float,decimal; - интеграция с generic math через
INumber<Rational<T>>.
Подключение
Добавьте ссылку на проект Rationalz:
<ProjectReference Include="src\Rationalz\Rationalz.csproj" />
Целевые платформы библиотеки: .NET 7, .NET 8, .NET 9, .NET 10.
Примеры
Создание дробей
using Rationalz;
Rational<int> a = new(6, -8); // -3/4
Rational<int> b = new(5); // 5
Rational<int> c = 42; // implicit conversion from int
Console.WriteLine(a); // -3/4
Console.WriteLine(b); // 5
Console.WriteLine(c); // 42
Арифметика
using Rationalz;
Rational<int> left = new(1, 2);
Rational<int> right = new(1, 3);
Rational<int> sum = left + right; // 5/6
Rational<int> difference = left - right; // 1/6
Rational<int> product = left * right; // 1/6
Rational<int> quotient = left / right; // 3/2
Rational<int> remainder = new Rational<int>(7, 3) % new Rational<int>(2, 3); // 1/3
Сравнение
using Rationalz;
Rational<int> x = new(1, 2);
Rational<int> y = new(2, 3);
bool less = x < y; // true
bool equals = x == y; // false
int order = x.CompareTo(y); // < 0
Парсинг и форматирование
using System.Globalization;
using Rationalz;
Rational<long> x = Rational<long>.Parse(" -10 / 20 ", CultureInfo.InvariantCulture);
bool parsed = Rational<int>.TryParse("7/8", CultureInfo.InvariantCulture, out Rational<int> y);
Console.WriteLine(x); // -1/2
Console.WriteLine(y.ToString("X", CultureInfo.InvariantCulture)); // 7/8
Преобразование к вещественным типам
using Rationalz;
Rational<int> value = new(3, 4);
double asDouble = (double)value; // 0.75
float asFloat = (float)value; // 0.75
decimal asDecimal = (decimal)value; // 0.75
Generic Math
using Rationalz;
static T Sum<T>(T left, T right)
where T : System.Numerics.INumber<T>
{
return left + right;
}
Rational<int> result = Sum(new Rational<int>(1, 2), new Rational<int>(1, 6));
Console.WriteLine(result); // 2/3
Поведение и ограничения
new Rational<T>(numerator, 0)выбрасываетDivideByZeroException;- если числитель равен нулю, дробь нормализуется к
0/1; - если знаменатель отрицательный, знак переносится в числитель;
TryConvertToCheckedвозвращаетfalseдля нецелых дробей;- тип
Tдолжен быть целочисленным, дробные типы в качествеTне поддерживаются.
Тесты
Запуск unit-тестов:
dotnet test .\tests\Rationalz.UnitTests\Rationalz.UnitTests.csproj
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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.
-
net10.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
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 |
|---|---|---|
| 1.0.0 | 102 | 4/3/2026 |