# Fractions 6.0.0

The fraction data type consists of two BigInteger values for numerator and denominator. It implements various operations (addition, subtraction, multiplication, division and remainder) with operator overloads.

`Install-Package Fractions -Version 6.0.0`
`dotnet add package Fractions --version 6.0.0`
`<PackageReference Include="Fractions" Version="6.0.0" />`
For projects that support PackageReference, copy this XML node into the project file to reference the package.
`paket add Fractions --version 6.0.0`
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
`#r "nuget: Fractions, 6.0.0"`
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
```// Install Fractions as a Cake Addin
#addin nuget:?package=Fractions&version=6.0.0

// Install Fractions as a Cake Tool
#tool nuget:?package=Fractions&version=6.0.0```
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

## Introduction

This package contains a data type to calculate with rational numbers. It supports basic mathematic operators such as:

• addition
• subtraction
• multiplication
• division
• remainder
• ..

The fraction data type implements operator overloads and implicit type conversion for convenience.

## Creation

You can implicitly cast `int`, `uint`, `long`, `ulong` or `BigInteger` to `Fraction`:

``````Fraction a = 3;  // int
Fraction b = 4L; // long
Fraction c = new BigInteger(3);
// ..
``````

You can explicitly cast `decimal` and `double` to `Fraction`:

``````var a = (Fraction)3.3m; // decimal
var b = (Fraction)3.3;  // double
``````

You can explicitly cast from `Fraction` to any supported data type (`int`, `uint`, `long`, `ulong`, `BigInteger`, `decimal`, `double`). However, be aware that an `OverflowException` will be thrown, if the target data type's boundary values are exceeded.

### Constructors

There a three types of constructors available:

• `new Fraction (<value>)` for `int`, `uint`, `long`, `ulong`, `BigInteger`, `decimal` and `double`.
• `new Fraction (<numerator>, <denominator>)` using `BigInteger` for numerator and denominator.
• `new Fraction (<numerator>, <denominator>, <reduce>)` using `BigInteger` for numerator and denominator + `bool` to indicate if the resulting fraction shall be normalized (reduced).

### Static creation methods

• `Fraction.FromDecimal(decimal)`
• `Fraction.FromDouble(double)`
• `Fraction.FromDoubleRounded(double)`
• `Fraction.FromString(string)` (using current culture)
• `Fraction.FromString(string, IFormatProvider)`
• `Fraction.FromString(string, NumberStyles, IFormatProvider)`
• `Fraction.TryParse(string, out Fraction)` (using current culture)
• `Fraction.TryParse(string, NumberStyles, IFormatProvider, out Fraction)`

### Creation from `double`

The `double` data type stores its values as 64bit floating point numbers that comply with IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic. `double` cannot store some binary fractions. For example, 1/10, which is represented precisely by .1 as a decimal fraction, is represented by .001100110011 as a binary fraction, with the pattern 0011 repeating to infinity. In this case, the floating-point value provides an imprecise representation of the number that it represents:

``````var value = Fraction.FromDouble(0.1);
/* Returns 3602879701896397/36028797018963968
* which is 0.10000000000000000555111512312578 */
Console.WriteLine(value);
``````

You can use the `Fraction.FromDoubleRounded(double)` method to avoid big numbers in numerator and denominator. But please keep in mind that the creation speed is significantly slower than using the pure value from `Fraction.FromDouble(double)`. Example:

``````var value = Fraction.FromDoubleRounded(0.1);
// Returns 1/10 which is 0.1
Console.WriteLine(value);
``````

### Creation from `string`

The following string patterns can be parsed:

• `[+/-]n` where n is an integer. Examples: +5, -6, 1234, 0
• `[+/-]n.m` where n and m are integers. The decimal point symbol depends on the system's culture settings. Examples: -4.3, 0.45
• `[+/-]n/[+/-]m` where n and m are integers. Examples: 1/2, -4/5, +4/-3, 32/100
Example:
``````var value = Fraction.FromString("1,5", new CultureInfo("de-DE"))
// Returns 3/2 which is 1.5
Console.WriteLine(value);
``````

You should consider the `TryParse` methods when reading numbers as text from user input. Furthermore it is best practice to always supply a culture information (e.g. `CultureInfo.InvariantCulture`). Otherwise you will sooner or later parse wrong numbers because of different decimal point symbols or included Thousands character.

## Conversion

You can convert a `Fraction` to any supported data type by calling:

• `.ToInt32()`
• `.ToUInt32()`
• `.ToInt64()`
• `.ToUInt64()`
• `.ToBigInteger()`
• `.ToDecimal()`
• `.ToDouble()`
• `.ToString()` (using current culture)
• `.ToString(string)` (using format string and the system's current culture)
• `.ToString(string,IFormatProvider)`

If the target's data type boundary values are exceeded the system will throw an `OverflowException`.

Example:

``````var rationalNumber = new Fraction(1, 3);
var value = rationalNumber.ToDecimal();
// result is 0.33333
Console.WriteLine(Math.Round(value, 5));
``````

### String format

Character Description
G General format: <numerator>/<denominator> e.g. 1/3
n Numerator
d Denominator
z The fraction as integer
r The positive remainder of all digits after the decimal point using the format: <numerator>/<denominator> or `string.Empty` if the fraction is a valid integer without digits after the decimal point.
m The fraction as mixed number e.g. 2 1/3 instead of 7/3

Note: The special characters #, and 0 like in #.### are not supported. Convert the `Fraction` to `decimal` if you want to display rounded decimal values.

Example:

``````var value = new Fraction(3, 2);
// returns 1 1/2
Console.WriteLine(value.ToString("m", new CultureInfo("de-DE")));
``````

## Mathematic operators

The following mathematic operations are supported:

• `.Reduce()` returns a normalized fraction (e.g. 2/41/2)
• `.Add(Fraction)` returns the sum of `(a + b)`
• `.Subtract(Fraction)` returns the difference of `(a - b)`
• `.Multiply(Fraction)` returns the product of `(a * b)`
• `.Divide(Fraction)` returns the quotient of `(a / b)`
• `.Remainder(Fraction)` returns the remainder (or left over) of `(a % b)`
• `.Invert()` returns an inverted fraction (same operation as `(a * -1)`)
• `.Abs()` returns the absolute value `|a|`
• `Fraction.Pow(Fraction, int)` returns a base raised to a power `(a ^ exponent)` (e.g. 1/10^(-1) → 10/1)

Example:

`````` var a = new Fraction(1, 3);
var b = new Fraction(2, 3);
var result = a * b;
// returns 2/9 which is 0,2222...
Console.WriteLine(result);
``````

## Equality operators

`Fraction` implements the following interfaces:

• `IEquatable<Fraction>`,
• `IComparable`,
• `IComparable<Fraction>`

Please note that `.Equals(Fraction)` will compare the exact values of numerator and denominator. That said:

``````var a = new Fraction(1, 2, true);
var b = new Fraction(1, 2, false);
var c = new Fraction(2, 4, false);

// result1 is true
var result1 = a == a;

// result2 is true
var result2 = a == b;

// result3 is false
var result3 = a == c;
``````

You have to use `.IsEquivalentTo(Fraction)` if want to test non-normalized fractions for value-equality.

## Under the hood

The data type stores the numerator and denominator as `BigInteger`. Per default it will reduce fractions to its normalized form during creation. The result of each mathematical operation will be reduced as well. There is a special constructor to create a non-normalized fraction. Be aware that `Equals` relies on normalized values when comparing two different instances.

## Build from source ### Required software frameworks

• .Net Core 3.1 SDK (LTS version)

### Required build tools

Please run:

`dotnet tool install fake-cli -g`

to install fake as global tool. On Linux you may have to add the following lines into your .profile or .bashrc file:

``````if [ -d "\$HOME/.dotnet/tools" ] ; then
PATH="\$HOME/.dotnet/tools:\$PATH"
fi
``````

Fractions uses the great FAKE DSL for build tasks. To build the solution, simply start the build.cmd on Windows or the build.sh shell script on Unix.

## Introduction

This package contains a data type to calculate with rational numbers. It supports basic mathematic operators such as:

• addition
• subtraction
• multiplication
• division
• remainder
• ..

The fraction data type implements operator overloads and implicit type conversion for convenience.

## Creation

You can implicitly cast `int`, `uint`, `long`, `ulong` or `BigInteger` to `Fraction`:

``````Fraction a = 3;  // int
Fraction b = 4L; // long
Fraction c = new BigInteger(3);
// ..
``````

You can explicitly cast `decimal` and `double` to `Fraction`:

``````var a = (Fraction)3.3m; // decimal
var b = (Fraction)3.3;  // double
``````

You can explicitly cast from `Fraction` to any supported data type (`int`, `uint`, `long`, `ulong`, `BigInteger`, `decimal`, `double`). However, be aware that an `OverflowException` will be thrown, if the target data type's boundary values are exceeded.

### Constructors

There a three types of constructors available:

• `new Fraction (<value>)` for `int`, `uint`, `long`, `ulong`, `BigInteger`, `decimal` and `double`.
• `new Fraction (<numerator>, <denominator>)` using `BigInteger` for numerator and denominator.
• `new Fraction (<numerator>, <denominator>, <reduce>)` using `BigInteger` for numerator and denominator + `bool` to indicate if the resulting fraction shall be normalized (reduced).

### Static creation methods

• `Fraction.FromDecimal(decimal)`
• `Fraction.FromDouble(double)`
• `Fraction.FromDoubleRounded(double)`
• `Fraction.FromString(string)` (using current culture)
• `Fraction.FromString(string, IFormatProvider)`
• `Fraction.FromString(string, NumberStyles, IFormatProvider)`
• `Fraction.TryParse(string, out Fraction)` (using current culture)
• `Fraction.TryParse(string, NumberStyles, IFormatProvider, out Fraction)`

### Creation from `double`

The `double` data type stores its values as 64bit floating point numbers that comply with IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic. `double` cannot store some binary fractions. For example, 1/10, which is represented precisely by .1 as a decimal fraction, is represented by .001100110011 as a binary fraction, with the pattern 0011 repeating to infinity. In this case, the floating-point value provides an imprecise representation of the number that it represents:

``````var value = Fraction.FromDouble(0.1);
/* Returns 3602879701896397/36028797018963968
* which is 0.10000000000000000555111512312578 */
Console.WriteLine(value);
``````

You can use the `Fraction.FromDoubleRounded(double)` method to avoid big numbers in numerator and denominator. But please keep in mind that the creation speed is significantly slower than using the pure value from `Fraction.FromDouble(double)`. Example:

``````var value = Fraction.FromDoubleRounded(0.1);
// Returns 1/10 which is 0.1
Console.WriteLine(value);
``````

### Creation from `string`

The following string patterns can be parsed:

• `[+/-]n` where n is an integer. Examples: +5, -6, 1234, 0
• `[+/-]n.m` where n and m are integers. The decimal point symbol depends on the system's culture settings. Examples: -4.3, 0.45
• `[+/-]n/[+/-]m` where n and m are integers. Examples: 1/2, -4/5, +4/-3, 32/100
Example:
``````var value = Fraction.FromString("1,5", new CultureInfo("de-DE"))
// Returns 3/2 which is 1.5
Console.WriteLine(value);
``````

You should consider the `TryParse` methods when reading numbers as text from user input. Furthermore it is best practice to always supply a culture information (e.g. `CultureInfo.InvariantCulture`). Otherwise you will sooner or later parse wrong numbers because of different decimal point symbols or included Thousands character.

## Conversion

You can convert a `Fraction` to any supported data type by calling:

• `.ToInt32()`
• `.ToUInt32()`
• `.ToInt64()`
• `.ToUInt64()`
• `.ToBigInteger()`
• `.ToDecimal()`
• `.ToDouble()`
• `.ToString()` (using current culture)
• `.ToString(string)` (using format string and the system's current culture)
• `.ToString(string,IFormatProvider)`

If the target's data type boundary values are exceeded the system will throw an `OverflowException`.

Example:

``````var rationalNumber = new Fraction(1, 3);
var value = rationalNumber.ToDecimal();
// result is 0.33333
Console.WriteLine(Math.Round(value, 5));
``````

### String format

Character Description
G General format: <numerator>/<denominator> e.g. 1/3
n Numerator
d Denominator
z The fraction as integer
r The positive remainder of all digits after the decimal point using the format: <numerator>/<denominator> or `string.Empty` if the fraction is a valid integer without digits after the decimal point.
m The fraction as mixed number e.g. 2 1/3 instead of 7/3

Note: The special characters #, and 0 like in #.### are not supported. Convert the `Fraction` to `decimal` if you want to display rounded decimal values.

Example:

``````var value = new Fraction(3, 2);
// returns 1 1/2
Console.WriteLine(value.ToString("m", new CultureInfo("de-DE")));
``````

## Mathematic operators

The following mathematic operations are supported:

• `.Reduce()` returns a normalized fraction (e.g. 2/41/2)
• `.Add(Fraction)` returns the sum of `(a + b)`
• `.Subtract(Fraction)` returns the difference of `(a - b)`
• `.Multiply(Fraction)` returns the product of `(a * b)`
• `.Divide(Fraction)` returns the quotient of `(a / b)`
• `.Remainder(Fraction)` returns the remainder (or left over) of `(a % b)`
• `.Invert()` returns an inverted fraction (same operation as `(a * -1)`)
• `.Abs()` returns the absolute value `|a|`
• `Fraction.Pow(Fraction, int)` returns a base raised to a power `(a ^ exponent)` (e.g. 1/10^(-1) → 10/1)

Example:

`````` var a = new Fraction(1, 3);
var b = new Fraction(2, 3);
var result = a * b;
// returns 2/9 which is 0,2222...
Console.WriteLine(result);
``````

## Equality operators

`Fraction` implements the following interfaces:

• `IEquatable<Fraction>`,
• `IComparable`,
• `IComparable<Fraction>`

Please note that `.Equals(Fraction)` will compare the exact values of numerator and denominator. That said:

``````var a = new Fraction(1, 2, true);
var b = new Fraction(1, 2, false);
var c = new Fraction(2, 4, false);

// result1 is true
var result1 = a == a;

// result2 is true
var result2 = a == b;

// result3 is false
var result3 = a == c;
``````

You have to use `.IsEquivalentTo(Fraction)` if want to test non-normalized fractions for value-equality.

## Under the hood

The data type stores the numerator and denominator as `BigInteger`. Per default it will reduce fractions to its normalized form during creation. The result of each mathematical operation will be reduced as well. There is a special constructor to create a non-normalized fraction. Be aware that `Equals` relies on normalized values when comparing two different instances.

## Build from source ### Required software frameworks

• .Net Core 3.1 SDK (LTS version)

### Required build tools

Please run:

`dotnet tool install fake-cli -g`

to install fake as global tool. On Linux you may have to add the following lines into your .profile or .bashrc file:

``````if [ -d "\$HOME/.dotnet/tools" ] ; then
PATH="\$HOME/.dotnet/tools:\$PATH"
fi
``````

Fractions uses the great FAKE DSL for build tasks. To build the solution, simply start the build.cmd on Windows or the build.sh shell script on Unix.

## Used By

### NuGet packages (4)

Showing the top 4 NuGet packages that depend on Fractions:

Package Downloads
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
Fractions.Json
Converts a fraction data type to JSON by using Fraction's ToString() and FromString() methods. The class name is JsonFractionConverter.
KubernetesClientDXBrazil
Client library for the Kubernetes open source container orchestrator.
Hilke.KineticConvolution
Implementation of the kinetic convolution of polygonal tracings in the Euclidian plane.

### GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Fractions:

Repository Stars
kubernetes-client/csharp
Work In Progress

## Version History

Version Downloads Last updated
6.0.0 1,855 2/18/2021
5.0.1 4,879 11/13/2020
5.0.0 117 11/13/2020
4.0.1 1,810,699 6/3/2019
4.0.0 344 6/3/2019
3.0.1 223,008 5/1/2017
3.0.0 607 5/1/2017
2.0.1 2,523 10/29/2015
2.0.0 685 10/29/2015
1.2.0 1,148 1/14/2015
1.1.0 1,881 2/16/2014
1.0.1 1,250 11/16/2013
1.0.0.1 826 4/21/2013
1.0.0 883 4/14/2013