bigint-fraction
Version:
Fraction composed of bigint
382 lines (381 loc) • 10.4 kB
TypeScript
/**
* Fractional number class.
*
* @implements {FractionLike}
* @implements {Reducible}
*/
export declare class Fraction implements FractionLike, Reducible {
/**
* Internal integer as denominator.
*
* @type {bigint}
*/
private _denominator;
/**
* Internal irreducible state.
* This value won't be defined until
* either reduce or reduceAsync is called.
*
* @type {boolean | undefined}
*/
private _irreducible?;
/**
* Internal integer as numerator.
*
* @type {bigint}
*/
private _numerator;
/**
* Constructs an instance of the fractional number class.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
* Either an object or an integer.
*
* If an object which contains numerator and denominator as the keys
* is specified, they will be copied to the new object. In this case,
* only the first one would be used no matter what the second argument.
*
* If an integer is specified, it and the second argument will become
* the numerator and the denominator of the new object.
*
* If this argument is omitted, the new object would be a zero.
*
* @param {bigint | number} denominator
*
* The denominator of the new fractional number object.
*
* If this argument is omitted, the new object would be just an integer.
*/
constructor(valueOrNumerator?: FractionLike | Integer, denominator?: Integer);
/**
* Increases the value of this object.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
* Either an object or an integer.
*
* If an object which has 'numerator' and 'denominator' as the keys
* is specified, their values will be used to increase the value of
* this object. In this case, only the first one will be used no matter
* what the second argument.
*
* If an integer is specified, it and the second argument will be used
* as the numerator and the denominator to increase the value of this
* object.
*
* This argument must not be omitted.
*
* @param {bigint | number} denominator
*
* An integer used as denominator to increase the value of this object.
*
* If this argument is omitted, the denominator will be interpreted as 1.
*/
add(valueOrNumerator: FractionLike | Integer, denominator?: Integer): void;
/**
* Increases the value of this object asynchronously.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
* Either an object or an integer.
*
* If an object which has 'numerator' and 'denominator' as the keys
* is specified, their values will be used to increase the value of
* this object. In this case, only the first one will be used no matter
* what the second argument.
*
* If an integer is specified, it and the second argument will be used
* as the numerator and the denominator to increase the value of this
* object.
*
* This argument must not be omitted.
*
* @param {bigint | number} denominator
*
* An integer used as denominator to increase the value of this object.
*
* If this argument is omitted, the denominator will be interpreted as 1.
*
* @returns {Promise<void>}
*
* A promise.
*/
addAsync(valueOrNumerator: FractionLike | Integer, denominator?: Integer): Promise<void>;
/**
* Creates a clone of this object.
*
* @returns {Fraction}
*
* Created object.
*/
clone(): Fraction;
/**
* Returns the denominator of this object.
*
* @type {bigint}
*/
get denominator(): bigint;
/**
* Divides the value of this object.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
*
* @param {bigint | number} denominator
*
*
*/
divide(valueOrNumerator: FractionLike | Integer, denominator?: Integer): void;
/**
* Divides the value of this object asynchronously.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
*
* @param {bigint | number} denominator
*
*
* @returns {Promise<void>}
*
* A promise.
*/
divideAsync(valueOrNumerator: FractionLike | Integer, denominator?: Integer): Promise<void>;
/**
* Determines whether if the value of this object is irreducible or not.
*
* @type {boolean}
*/
get isIrreducible(): boolean;
/**
* Multiplys the value of this object.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
*
* @param {bigint | number} denominator
*
*
*/
multiply(valueOrNumerator: FractionLike | Integer, denominator?: Integer): void;
/**
* Multiplys the value of this object asynchronously.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
*
* @param {bigint | number} denominator
*
*
* @returns {Promise<void>}
*
* A promise.
*/
multiplyAsync(valueOrNumerator: FractionLike | Integer, denominator?: Integer): Promise<void>;
/**
* Returns the numerator of this object.
*
* @type {bigint}
*/
get numerator(): bigint;
/**
* Makes this object irreducible.
*
* @param {Function} cb
*
* Callback function which takes a greatest common divisor.
*
* @returns {Irreducible | T}
*
* If this is already irreducible, the instance of Irreducible class
* will be returned. Otherwise, the value returned by the callback
* function will be returned.
*/
reduce<T>(cb?: (gcd: bigint) => T): Irreducible | T;
/**
* Makes this object irreducible asynchronously.
*
* @param {Function} cb
*
* Callback function which takes a greatest common divisor.
*
* @returns {Promise<Irreducible | T>}
*
* A promise.
*/
reduceAsync<T>(cb: ReduceAsyncCallback<T>): Promise<Irreducible | T>;
/**
* Decrease the value of this object.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
*
* @param {bigint | number} denominator
*
*
*/
subtract(valueOrNumerator: FractionLike | Integer, denominator?: Integer): void;
/**
* Decrease the value of this object asynchronously.
*
* @param {Fraction | FractionLike | bigint | number} valueOrNumerator
*
*
* @param {bigint | number} denominator
*
*
* @returns {Promise<void>}
*
* A promise.
*/
subtractAsync(valueOrNumerator: FractionLike | Integer, denominator?: Integer): Promise<void>;
/**
* Converts to a string of decimal value.
*
* @param {number} precision
*
* A precision.
*
* @returns {string}
*
* The converted string of decimal value.
*/
toString(precision?: number): string;
}
/**
* A couple of 'bigint' which composes the fractional number.
*/
export type FractionLike = {
/**
* An integer of denominator.
*
* @type {bigint}
*/
denominator: bigint;
/**
* An integer of numerator.
*
* @type {bigint}
*/
numerator: bigint;
};
/**
* Integer.
*/
export type Integer = bigint | number;
/**
* Irreducible class.
*
* @implements {Reducible}
*/
export declare class Irreducible implements Reducible {
/**
* The sole instance of Irreducible class.
*
* @type {Irreducible}
*/
static readonly TheInstance: Reducible;
/**
* Constructs an object of Irreducible class.
* This is never used externally.
*/
private constructor();
/**
* Always true.
*
* @type {boolean}
*/
get isIrreducible(): boolean;
/**
* Does nothing.
*
* @param {Function} cb
*
* A callback function, but never called
*
* @returns
*
* The sole instance of Irreducible class.
*/
reduce<T>(cb?: (gcd: bigint) => T): Irreducible | T;
/**
* Does nothing.
*
* @param {Function} cb
*
* A callback function, but never called
*
* @returns
*
* The sole instance of Irreducible class
*/
reduceAsync<T>(cb: ReduceAsyncCallback<T>): Promise<Irreducible | T>;
}
/**
* Callback function type for Reducible#reduceAsync.
*/
type ReduceAsyncCallback<T> = (gcd: bigint) => PromiseLike<T> | T;
/**
* Reducible interface.
*/
export interface Reducible {
/**
* Determines whether if the value of this object is irreducible or not.
*
* @type {boolean}
*/
get isIrreducible(): boolean;
/**
* Makes this object irreducible.
*
* @param {Function} cb
*
* Callback function which takes a greatest common divisor.
*
* @returns {Irreducible | T}
*
* If this is already irreducible, the instance of Irreducible class
* will be returned. Otherwise, the value returned by the callback
* function will be returned.
*/
reduce<T>(cb?: (gcd: bigint) => T): Irreducible | T;
/**
* Makes this object irreducible asynchronously.
*
* @param {Function} cb
*
* Callback function which takes a greatest common divisor.
*
* @returns {Irreducible | T}
*
* If this is already irreducible, the instance of Irreducible class
* will be returned. Otherwise, the value returned by the callback
* function will be returned.
*/
reduceAsync<T>(cb: ReduceAsyncCallback<T>): Promise<Irreducible | T>;
}
/**
* Call procedures concurrently.
*
* @param {Function[]} procedures
*
* An array of procedures.
*
* @returns {Promise<T[]>}
*
* A promise.
*/
export declare const callProceduresConcurrentlyAsync: <T>(...procedures: (() => T)[]) => Promise<T[]>;
/**
* Determines whether if a value is similar to the fraction class.
*
* @param {unknown} value
*
* A target value to be determined.
*
* @returns {boolean}
*
* Type-guard-specifier for FractionLike.
*/
export declare const isFractionLike: (value: unknown) => value is FractionLike;
export {};