Table of Contents

Class NativeFraction

Namespace
Hi.Geom
Assembly
HiDisp.dll

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

src NativeFraction

The 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

val double

The double value to convert.

resolution double

The resolution tolerance for approximation.

NativeFraction(int)

public NativeFraction(int num)

Parameters

num int

NativeFraction(long)

Initializes a new instance with integer value.

public NativeFraction(long num)

Parameters

num long

The integer numerator value.

NativeFraction(long, long)

Initializes a new instance with numerator and denominator.

public NativeFraction(long num, long den)

Parameters

num long

The numerator.

den long

The denominator. Cannot be negative.

Properties

CeilInt

Gets the ceiling integer value.

public int CeilInt { get; }

Property Value

int

Denominator

Gets or sets the denominator as BigInteger. Uses byte array transfer for better performance.

public BigInteger Denominator { get; set; }

Property Value

BigInteger

DenominatorString

Gets or sets the denominator as a string (for unlimited precision).

public string DenominatorString { get; set; }

Property Value

string

FloorInt

Gets the floor integer value.

public int FloorInt { get; }

Property Value

int

IsEvaluated

Gets whether the fraction value has been evaluated.

public bool IsEvaluated { get; }

Property Value

bool

IsFinite

Gets whether the fraction is finite (denominator != 0).

public bool IsFinite { get; }

Property Value

bool

IsNaN

Gets whether the fraction is NaN (0/0).

public bool IsNaN { get; }

Property Value

bool

IsPacked

Gets whether the fraction is packed (reduced to irreducible form).

public bool IsPacked { get; }

Property Value

bool

IsZero

Gets whether the fraction is zero (numerator == 0 and denominator != 0).

public bool IsZero { get; }

Property Value

bool

Numerator

Gets or sets the numerator as BigInteger. Uses byte array transfer for better performance.

public BigInteger Numerator { get; set; }

Property Value

BigInteger

NumeratorString

Gets or sets the numerator as a string (for unlimited precision).

public string NumeratorString { get; set; }

Property Value

string

Ptr

Gets the native pointer.

public nint Ptr { get; }

Property Value

nint

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

double

Sign

Gets the sign of the fraction (-1, 0, or 1).

public int Sign { get; }

Property Value

int

Status

Gets the status flags.

public FractionStatus Status { get; }

Property Value

FractionStatus

Value

Gets the evaluated double value. Computes the value if not yet evaluated.

public double Value { get; }

Property Value

double

Methods

Abs()

Gets the absolute value of this fraction.

public NativeFraction Abs()

Returns

NativeFraction

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

iterations int

Number of accumulation steps per round.

rounds int

Number of rounds to average timing.

CompareTo(NativeFraction)

Compares this fraction with another.

public int CompareTo(NativeFraction other)

Parameters

other NativeFraction

The 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

disposing bool

Equals(object)

Determines whether the specified object is equal to the current object.

public override bool Equals(object obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

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

NativeFraction

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

NativeFraction

One()

Creates a one fraction (1/1).

public static NativeFraction One()

Returns

NativeFraction

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

iterations int

Number of steps per round.

rounds int

Number of rounds to average timing.

PositiveInf()

Creates a positive infinity fraction (1/0).

public static NativeFraction PositiveInf()

Returns

NativeFraction

Reciprocal()

Gets the reciprocal of this fraction.

public NativeFraction Reciprocal()

Returns

NativeFraction

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

numerator BigInteger

The numerator value.

denominator BigInteger

The 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

resolution double

The resolution tolerance.

Returns

NativeFraction

This instance for chaining.

Square()

Gets the square of this fraction.

public NativeFraction Square()

Returns

NativeFraction

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

val double

The double value.

resolution double

The resolution tolerance.

Returns

NativeFraction

Zero()

Creates a zero fraction (0/1).

public static NativeFraction Zero()

Returns

NativeFraction

Operators

operator +(NativeFraction, NativeFraction)

Addition operator.

public static NativeFraction operator +(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

NativeFraction

operator /(NativeFraction, NativeFraction)

Division operator.

public static NativeFraction operator /(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

NativeFraction

operator ==(NativeFraction, NativeFraction)

Equality operator.

public static bool operator ==(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

bool

explicit operator double(NativeFraction)

Explicit conversion to double.

public static explicit operator double(NativeFraction f)

Parameters

f NativeFraction

Returns

double

operator >(NativeFraction, NativeFraction)

Greater than operator.

public static bool operator >(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

bool

operator >=(NativeFraction, NativeFraction)

Greater than or equal operator.

public static bool operator >=(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

bool

operator !=(NativeFraction, NativeFraction)

Inequality operator.

public static bool operator !=(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

bool

operator <(NativeFraction, NativeFraction)

Less than operator.

public static bool operator <(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

bool

operator <=(NativeFraction, NativeFraction)

Less than or equal operator.

public static bool operator <=(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

bool

operator *(NativeFraction, NativeFraction)

Multiplication operator.

public static NativeFraction operator *(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

NativeFraction

operator -(NativeFraction, NativeFraction)

Subtraction operator.

public static NativeFraction operator -(NativeFraction a, NativeFraction b)

Parameters

a NativeFraction
b NativeFraction

Returns

NativeFraction

operator -(NativeFraction)

Negation operator.

public static NativeFraction operator -(NativeFraction a)

Parameters

a NativeFraction

Returns

NativeFraction