Class NativeFraction
Native wrapper for C++ fraction_t<0> (unlimited precision fraction). A fraction consists of a numerator and denominator using unlimited precision integers.
public class NativeFraction : IDisposable
- Inheritance
-
NativeFraction
- Implements
- Inherited Members
- Extension Methods
Remarks
Delegate to geom::fraction_t<0> (fraction_unlimited_t) in CppCore.
The fraction may be not packed or not evaluated. However, all fraction numerator and denominator are singular managed and denominator is never negative.
Constructors
NativeFraction()
Initializes a new instance of zero fraction.
public NativeFraction()
NativeFraction(NativeFraction)
Initializes a new instance by copying another fraction.
public NativeFraction(NativeFraction src)
Parameters
srcNativeFractionThe source fraction to copy.
NativeFraction(double, double)
Initializes a new instance from a double value with specified resolution. Uses Stern-Brocot binary search for approximation.
public NativeFraction(double val, double resolution)
Parameters
valdoubleThe double value to convert.
resolutiondoubleThe resolution tolerance for approximation.
NativeFraction(int)
public NativeFraction(int num)
Parameters
numint
NativeFraction(long)
Initializes a new instance with integer value.
public NativeFraction(long num)
Parameters
numlongThe integer numerator value.
NativeFraction(long, long)
Initializes a new instance with numerator and denominator.
public NativeFraction(long num, long den)
Parameters
Properties
CeilInt
Gets the ceiling integer value.
public int CeilInt { get; }
Property Value
Denominator
Gets or sets the denominator as BigInteger. Uses byte array transfer for better performance.
public BigInteger Denominator { get; set; }
Property Value
DenominatorString
Gets or sets the denominator as a string (for unlimited precision).
public string DenominatorString { get; set; }
Property Value
FloorInt
Gets the floor integer value.
public int FloorInt { get; }
Property Value
IsEvaluated
Gets whether the fraction value has been evaluated.
public bool IsEvaluated { get; }
Property Value
IsFinite
Gets whether the fraction is finite (denominator != 0).
public bool IsFinite { get; }
Property Value
IsNaN
Gets whether the fraction is NaN (0/0).
public bool IsNaN { get; }
Property Value
IsPacked
Gets whether the fraction is packed (reduced to irreducible form).
public bool IsPacked { get; }
Property Value
IsZero
Gets whether the fraction is zero (numerator == 0 and denominator != 0).
public bool IsZero { get; }
Property Value
Numerator
Gets or sets the numerator as BigInteger. Uses byte array transfer for better performance.
public BigInteger Numerator { get; set; }
Property Value
NumeratorString
Gets or sets the numerator as a string (for unlimited precision).
public string NumeratorString { get; set; }
Property Value
Ptr
Gets the native pointer.
public nint Ptr { get; }
Property Value
RoughValue
Gets the roughly evaluated double value. If the data has not been reduced, the return value is not evaluated by the reduced numbers.
public double RoughValue { get; }
Property Value
Sign
Gets the sign of the fraction (-1, 0, or 1).
public int Sign { get; }
Property Value
Status
Gets the status flags.
public FractionStatus Status { get; }
Property Value
Value
Gets the evaluated double value. Computes the value if not yet evaluated.
public double Value { get; }
Property Value
Methods
Abs()
Gets the absolute value of this fraction.
public NativeFraction Abs()
Returns
ComparePerformanceTest(int, int)
Comparative performance test between pure C# Fraction and NativeFraction (C++ backend).
public static void ComparePerformanceTest(int iterations = 128, int rounds = 16)
Parameters
iterationsintNumber of accumulation steps per round.
roundsintNumber of rounds to average timing.
CompareTo(NativeFraction)
Compares this fraction with another.
public int CompareTo(NativeFraction other)
Parameters
otherNativeFractionThe other fraction.
Returns
- int
-1 if less, 0 if equal, 1 if greater.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
Equals(object)
Determines whether the specified object is equal to the current object.
public override bool Equals(object obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Evaluate()
Evaluates the double value if not already evaluated.
public NativeFraction Evaluate()
Returns
- NativeFraction
This instance for chaining.
~NativeFraction()
protected ~NativeFraction()
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
NaN()
Creates a NaN fraction (0/0).
public static NativeFraction NaN()
Returns
Negate()
Negates this fraction in place.
public NativeFraction Negate()
Returns
- NativeFraction
This instance for chaining.
NegativeInf()
Creates a negative infinity fraction (-1/0).
public static NativeFraction NegativeInf()
Returns
One()
Creates a one fraction (1/1).
public static NativeFraction One()
Returns
Pack()
Packs (reduces) the fraction to irreducible form if not already packed.
public NativeFraction Pack()
Returns
- NativeFraction
This instance for chaining.
PerformanceTest(int, int)
Performance test for NativeFraction (C++ backend). Test 1: Accumulative += with Val (bounded, linear growth). Test 2: Bounded arithmetic (converging average).
public static void PerformanceTest(int iterations = 128, int rounds = 16)
Parameters
PositiveInf()
Creates a positive infinity fraction (1/0).
public static NativeFraction PositiveInf()
Returns
Reciprocal()
Gets the reciprocal of this fraction.
public NativeFraction Reciprocal()
Returns
SetAbs()
Sets this fraction to its absolute value.
public NativeFraction SetAbs()
Returns
- NativeFraction
This instance for chaining.
SetNumeratorAndDenominator(BigInteger, BigInteger)
Sets both numerator and denominator at once.
public void SetNumeratorAndDenominator(BigInteger numerator, BigInteger denominator)
Parameters
numeratorBigIntegerThe numerator value.
denominatorBigIntegerThe denominator value.
SetReciprocal()
Sets this fraction to its reciprocal.
public NativeFraction SetReciprocal()
Returns
- NativeFraction
This instance for chaining.
SetSquare()
Sets this fraction to its square.
public NativeFraction SetSquare()
Returns
- NativeFraction
This instance for chaining.
Simplify(double)
Simplifies the fraction to the specified resolution.
public NativeFraction Simplify(double resolution)
Parameters
resolutiondoubleThe resolution tolerance.
Returns
- NativeFraction
This instance for chaining.
Square()
Gets the square of this fraction.
public NativeFraction Square()
Returns
Test()
Test function for NativeFraction.
public static void Test()
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
Val(double, double)
Creates a fraction from a double value with specified resolution.
public static NativeFraction Val(double val, double resolution)
Parameters
Returns
Zero()
Creates a zero fraction (0/1).
public static NativeFraction Zero()
Returns
Operators
operator +(NativeFraction, NativeFraction)
Addition operator.
public static NativeFraction operator +(NativeFraction a, NativeFraction b)
Parameters
Returns
operator /(NativeFraction, NativeFraction)
Division operator.
public static NativeFraction operator /(NativeFraction a, NativeFraction b)
Parameters
Returns
operator ==(NativeFraction, NativeFraction)
Equality operator.
public static bool operator ==(NativeFraction a, NativeFraction b)
Parameters
Returns
explicit operator double(NativeFraction)
Explicit conversion to double.
public static explicit operator double(NativeFraction f)
Parameters
Returns
operator >(NativeFraction, NativeFraction)
Greater than operator.
public static bool operator >(NativeFraction a, NativeFraction b)
Parameters
Returns
operator >=(NativeFraction, NativeFraction)
Greater than or equal operator.
public static bool operator >=(NativeFraction a, NativeFraction b)
Parameters
Returns
operator !=(NativeFraction, NativeFraction)
Inequality operator.
public static bool operator !=(NativeFraction a, NativeFraction b)
Parameters
Returns
operator <(NativeFraction, NativeFraction)
Less than operator.
public static bool operator <(NativeFraction a, NativeFraction b)
Parameters
Returns
operator <=(NativeFraction, NativeFraction)
Less than or equal operator.
public static bool operator <=(NativeFraction a, NativeFraction b)
Parameters
Returns
operator *(NativeFraction, NativeFraction)
Multiplication operator.
public static NativeFraction operator *(NativeFraction a, NativeFraction b)
Parameters
Returns
operator -(NativeFraction, NativeFraction)
Subtraction operator.
public static NativeFraction operator -(NativeFraction a, NativeFraction b)
Parameters
Returns
operator -(NativeFraction)
Negation operator.
public static NativeFraction operator -(NativeFraction a)