Data and Math
vfraction
Exact fractions that stay reduced, so you never tidy them by hand.
Released v1.0.0 · 2026-09-28use "stdlib/fraction.ven"
Exact fractions as their own shape, with a numerator and a denominator field, kept reduced automatically rather than left for the caller to simplify by hand. It is built from ordinary integer arithmetic, with no native rational number type underneath it yet, and covers addition, subtraction, multiplication, division and comparison.
f = Fraction(2, 4)
print f.numerator
:: prints: 2
print f.denominator
:: prints: 4
Reference (16 functions)
API reference
frac_gcd(a, b)
Gives the greatest common divisor of a and b via the Euclidean algorithm.
frac_gcd(48, 18)
:: gives 6frac_new(numerator, denominator)
Builds a Fraction reduced to lowest terms, with a positive denominator.
print(frac_display(frac_new(2, 4)))
:: prints: 1/2frac_coerce(value)
Coerces a plain integer Number or an existing Fraction into a Fraction.
print(frac_display(frac_coerce(5)))
:: prints: 5frac_add(a, b)
Adds two Fractions (or a Fraction and a plain integer Number).
print(frac_display(frac_add(frac_new(1, 2), frac_new(1, 3))))
:: prints: 5/6frac_subtract(a, b)
Subtracts b from a (or a plain integer Number from a Fraction).
print(frac_display(frac_subtract(frac_new(1, 2), frac_new(1, 3))))
:: prints: 1/6frac_multiply(a, b)
Multiplies two Fractions (or a Fraction and a plain integer Number).
print(frac_display(frac_multiply(frac_new(2, 3), frac_new(3, 4))))
:: prints: 1/2frac_divide(a, b)
Divides a by b (or a Fraction by a plain integer Number).
print(frac_display(frac_divide(frac_new(1, 2), frac_new(1, 4))))
:: prints: 2frac_negate(f)
Negates a Fraction, flipping the sign of its numerator.
print(frac_display(frac_negate(frac_new(3, 4))))
:: prints: -3/4frac_reciprocal(f)
Gives the reciprocal of a Fraction, swapping numerator and denominator.
print(frac_display(frac_reciprocal(frac_new(3, 4))))
:: prints: 4/3frac_equals(a, b)
Tests two Fractions (or a Fraction and a plain integer Number) for equality.
frac_equals(frac_new(1, 2), frac_new(2, 4))
:: gives yesfrac_less_than(a, b)
Tests whether a is strictly less than b (or a plain integer Number).
frac_less_than(frac_new(1, 3), frac_new(1, 2))
:: gives yesfrac_compare(a, b)
Three-way compares a against b (or a plain integer Number).
frac_compare(frac_new(1, 2), frac_new(1, 3))
:: gives 1frac_to_number(f)
Converts a Fraction to the nearest Number, via true division.
frac_to_number(frac_new(1, 4))
:: gives 0.25frac_from_text(text)
print(frac_display(frac_from_text("3/4")))
:: prints: 3/4frac_display(f)
print(frac_display(frac_from_text("5")))
:: prints: 5