Fraction

References
[IoId("00000000-DF18-4DBC-951A-94ACCDF1B835")] public struct Fraction : IStorable
Represents a fraction.

Constructors

Fraction

#
public Fraction(BigInteger numerator, BigInteger denominator)
Create a new fraction from numerator and denominator.
numerator (BigInteger)Fraction numerator.
denominator (BigInteger)Fraction denominator.

Fraction

#
public Fraction(IReader reader)
reader (IReader)

Properties

Denominator

#
public BigInteger Denominator { get; }
Gets the denominator of this fraction.

IsNegative

#
public bool IsNegative { get; }
Gets whether this fraction is negative.

IsPositive

#
public bool IsPositive { get; }
Gets whether this fraction is negative.

IsUndefined

#
public bool IsUndefined { get; }
Gets whether this fraction is undefined (i.e. whether the denominator is zero).

IsZero

#
public bool IsZero { get; }
Gets whether this fraction has been initialised.

Magnitude

#
public BigInteger Magnitude { get; }
Gets the magnitude or integral part of this fraction. If the fraction is negative, the integral part will be more positive than the fraction value.

MinusOne

#
public static Fraction MinusOne { get; }
Gets the default negative unit value.

Numerator

#
public BigInteger Numerator { get; }
Gets the numerator of this fraction.

One

#
public static Fraction One { get; }
Gets the default positive unit value.

Partial

#
public Fraction Partial { get; }
Gets the partial part of this fraction. That is, the part after subtracting the magnitude.

Unset

#
public static Fraction Unset { get; }
Gets the default zero value.

Zero

#
public static Fraction Zero { get; }
Gets the default zero value.

Methods

Absolute

#
public Fraction Absolute()
Gets the absolute value of this fraction.

Equals

#
public override bool Equals(object obj)
Indicates whether this instance and a specified object are equal.
obj (object)The object to compare with the current instance.
ValueType.Equals

GetHashCode

#
public override int GetHashCode()
Returns the hash code for this instance.
A 32-bit signed integer that is the hash code for this instance.

Mean

#
public static Fraction Mean(Fraction a, Fraction b)
Compute the mean (or average) of two fractions.
a (Fraction)First fraction.
b (Fraction)Second fraction.
Mean.

Mediant

#
public static Fraction Mediant(Fraction a, Fraction b)
Compute the mediant of two fractions.
a (Fraction)First fraction.
b (Fraction)Second fraction.
Mediant.

Rationalise

#
public static Fraction Rationalise(double value, int maximumNumerator, int maximumDenominator)
Find a fraction which well describes this double.
value (double)Value to rationalise.
maximumNumerator (int)Upper numerator limit.
maximumDenominator (int)Upper denominator limit.
Best approximate fraction.

Rationalise

#
public static Fraction Rationalise(double value, params int[] allowedFractionElements)
Find a fraction which well describes this double.
value (double)Value to rationalise.
allowedFractionElements (params int[])SORTED! array of allowed numerators and denominators of the partial fraction.
Best approximate fraction or UNSET if rationalisation is not possible within the given constraints.

RationalMediants

#
public static IEnumerable<Fraction> RationalMediants(double target)
Re-implementation of Eric Lippert's dedoublifier algorithm at: https://ericlippert.com/2015/12/10/the-dedoublifier-part-four/
target (double)Double value to approximate, must be in the range [0, 1).
Sequence of rationals that (over time) approximate x.

Reduce

#
public Fraction Reduce()
Reduce this fraction by reducing the numerator and denominator by the greatest common denominator.
Reduced fraction.

Store

#
public void Store(IWriter writer)
writer (IWriter)
Store

ToDouble

#
public double ToDouble()
Get the double represented by this fraction.

ToString

#
public override string ToString()
Gets the string representation of this fraction.
String.

ToString

#
public string ToString(bool reduceToInteger, bool separateMagnitude)
Gets the string representation of this fraction.
reduceToInteger (bool)If true, the value will be formatted as a regular integer if it is one.
separateMagnitude (bool)If true, the magnitude will be included an a separate integer.
String.

Operators

OperationResultDescription
Fraction+FractionFractionAdd two fractions. The result is not necessarily in reduced form, as that may be a very slow operation.
Fraction/FractionFractionDivide two fractions. The result is not necessarily in reduced form, as that may be a very slow operation.
Fraction==FractionboolCompare two fractions. Equality is defined numerically, not structurally. Undefined fractions are always unequal.
Fraction>FractionboolCompare two fractions.
Fraction>=FractionboolCompare two fractions.
Fraction!=FractionboolCompare two fractions. Inequality is defined numerically, not structurally. Undefined fractions are always unequal.
Fraction<FractionboolCompare two fractions.
Fraction<=FractionboolCompare two fractions.
Fraction*FractionFractionMultiply two fractions. The result is not necessarily in reduced form, as that may be a very slow operation.
Fraction-FractionFractionSubtract two fractions. The result is not necessarily in reduced form, as that may be a very slow operation.

Convertors

OperationResultDescription
implicitFractiondoubleAuto convert a fraction to a double.
implicitBigIntegerFractionAuto convert an integer into a fraction.
implicitintFractionAuto convert an integer into a fraction.
implicitlongFractionAuto convert an integer into a fraction.