precise-fractions
Version:
Allows for working with fractions.
121 lines (118 loc) • 3.89 kB
TypeScript
/**
* Represents a fraction with numerator and denominator.
*
* Cast it to a number to get the fractional value as a Javascript number type.
*
* @property {bigint} numerator
* @property {bigint} denominator
*
*/
declare class Fraction {
numerator: bigint;
denominator: bigint;
/**
* Create a fraction
*
* @param numerator bigint | number | string
* @param denominator [bigint | number | string]
*/
constructor(numerator: bigint | number | string, denominator?: bigint | number | string);
valueOf(): number;
toString(): string;
/**
* Add one ore more fractions or valuee to the caller
*
* @param fractions Array with at least 1 of type Fraction | bigint | number | string
*/
add(...fractions: [Fraction | bigint | number | string, ...Array<Fraction | bigint | number | string>]): void;
/**
* Subtract a fraction or value from the caller
*
* @param f Fraction | bigint | number | string
*/
subtract(f: Fraction | bigint | number | string): void;
/**
* Multiply one ore more fractions or values
*
* @param fractions Array with at least 1 of type Fraction | bigint | number | string
*/
multiply(...fractions: [Fraction | bigint | number | string, ...Array<Fraction | bigint | number | string>]): void;
/**
* Divide by another fraction or value
*
* @param f Fraction | bigint | number | string
*/
divide(f: Fraction | bigint | number | string): void;
/**
* Raise the fraction to a power
*
* @param p
*/
power(p: bigint | number | string): void;
/**
* Check for equality after shortening the fraction
*
* @param f
* @returns
*/
equals(f: Fraction | bigint | number | string): boolean;
/**
* Shortens the fraction to the smallest possible form.
* If using the methods provided by the libary, this is done automatically.
*
* @param newInstance Whether to create a new instance of the fraction or to modify the current instance.
*/
shorten(newInstance: true): Fraction;
shorten(newInstance: false): void;
shorten(newInstance: boolean): Fraction | void;
shorten(newInstance?: undefined): void;
}
/**
* Create a new fraction.
*
* If the denominator is omitted, it becomes 1 by default.
*
* @param numerator bigint | number | string
* @param denominator bigint | number | string
* @returns Fraction
*/
declare function createFraction(numerator: bigint | number | string, denominator?: bigint | number | string): Fraction;
/**
* Add two or more fractions
*
* @param fractions Array with at least 1 of type Fraction | bigint | number | string
* @returns
*/
declare function add(...fractions: [Fraction | bigint | number | string, ...Array<Fraction | bigint | number | string>]): Fraction;
/**
* Subtract a fraction from another
*
* @param f1 Fraction
* @param f2 Fraction
* @returns Fraction
*/
declare function subtract(f1: Fraction | bigint | number | string, f2: Fraction | bigint | number | string): Fraction;
/**
* Multiply two or more fractions
*
* @param fractions Array with at least 1 of type Fraction | bigint | number | string
* @returns
*/
declare function multiply(...fractions: [Fraction | bigint | number | string, ...Array<Fraction | bigint | number | string>]): Fraction;
/**
* Divide a fraction by another
*
* @param f1 Fraction
* @param f2 Fraction
* @returns Fraction
*/
declare function divide(f1: Fraction | bigint | number | string, f2: Fraction | bigint | number | string): Fraction;
/**
* Raise a fraction to a power
*
* @param f Fraction
* @param power Exponent
* @returns Fraction
*/
declare function power(f: Fraction | bigint | number | string, power: bigint | number | string): Fraction;
export { Fraction, add, createFraction, divide, multiply, power, subtract };