Num.Frac
Num / Frac
Import
import { Num } from '@wollybeard/kit'
// Access via namespace
Num.Frac.someFunction()
import * as Num from '@wollybeard/kit/num'
// Access via namespace
Num.Frac.someFunction()
Functions
[F]
is
(value: unknown): boolean
Parameters:
value
- The value to check
Returns: True if value is a proper fraction (0 n/d 1)
Type predicate to check if value is a proper fraction.
[F]
from
(numerator: Natural, denominator: Natural): Frac
Parameters:
numerator
- The top number (positive, less than denominator)denominator
- The bottom number (positive, greater than numerator)
Returns: The fraction
Throws:
- Error if not a proper fraction
Construct a Fraction from numerator and denominator. Both must be positive and numerator must be less than denominator.
[F]
tryFrom
(numerator: Natural, denominator: Natural): Frac | null
Parameters:
numerator
- The top numberdenominator
- The bottom number
Returns: The fraction or null
Try to construct a Fraction. Returns null if not a proper fraction.
[F]
fromDecimal
(value: number, maxDenominator?: number = 100): Frac
Parameters:
value
- The decimal value (0 value 1)maxDenominator
- Maximum denominator to use (default: 100)
Returns: The fraction approximation
Throws:
- Error if value is not between 0 and 1
Convert a decimal to a fraction. The decimal must be between 0 and 1 (exclusive).
[F]
toDecimal
(frac: Frac): number
Parameters:
frac
- The fraction to convert
Returns: The decimal value (between 0 and 1)
Convert fraction to decimal.
[F]
toPercentage
(frac: Frac): number
Parameters:
frac
- The fraction to convert
Returns: The percentage value (0-100)
Convert fraction to percentage.
[F]
complement
(frac: Frac): Frac
Parameters:
frac
- The fraction
Returns: The complement as a fraction
Get the complement of a fraction (1
- fraction).
[F]
add
(a: Frac, b: Frac): Ratio
Parameters:
a
- First fractionb
- Second fraction
Returns: The sum as a Ratio (might be = 1)
Add two fractions. Note: The result might not be a fraction if the sum = 1.
[F]
multiply
(a: Frac, b: Frac): Frac
Parameters:
a
- First fractionb
- Second fraction
Returns: The product as a fraction
Multiply two fractions. The result is always a fraction (product of two numbers
1 is
1).
[F]
compare
(a: Frac, b: Frac): 0 | 1 | -1
Parameters:
a
- First fractionb
- Second fraction
Returns: -1 if a b, 0 if a = b, 1 if a b
Compare two fractions.
Constants
[C]
fromWith
;((numerator: Natural) => (denominator: Natural) => Frac)
Create a function that constructs fractions with a fixed numerator. Useful for creating series of fractions.
[C]
fromOn
;((denominator: Natural) => (numerator: Natural) => Frac)
Create a function that constructs fractions with a fixed denominator. Useful for working with common denominators.
[C]
addOn
;((a: Frac) => (b: Frac) => Ratio)
Create a function that adds to a specific fraction. Data-first pattern: fix the first argument.
[C]
addWith
;((b: Frac) => (a: Frac) => Ratio)
Create a function that adds with a specific fraction. Data-second pattern: fix the second argument.
[C]
multiplyOn
;((a: Frac) => (b: Frac) => Frac)
Create a function that multiplies a specific fraction. Data-first pattern: fix the first argument.
[C]
multiplyWith
;((b: Frac) => (a: Frac) => Frac)
Create a function that multiplies with a specific fraction. Data-second pattern: fix the second argument.
[C]
compareOn
;((a: Frac) => (b: Frac) => 0 | 1 | -1)
Create a function that compares a specific fraction. Data-first pattern: fix the first argument.
[C]
compareWith
;((b: Frac) => (a: Frac) => 0 | 1 | -1)
Create a function that compares with a specific fraction. Data-second pattern: fix the second argument.
Types
[∩]
Frac
type Frac = Ratio & { [FracBrand]: true }
Fraction (proper fraction)
- a positive ratio where 0
numerator
denominator.
Fractions represent parts of a whole, always between 0 and 1 (exclusive). They're ideal for:
- Probabilities (1/6 for dice roll)
- Portions and percentages (3/4 of a pizza)
- UI measurements (2/3 width)
- Musical note durations (1/4 note, 1/8 note)