Data and Math

vfraction

Exact fractions that stay reduced, so you never tidy them by hand.

Released v1.0.0 · 2026-09-28
Import
use "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.

vfraction.ven
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 6

frac_new(numerator, denominator)

Builds a Fraction reduced to lowest terms, with a positive denominator.

print(frac_display(frac_new(2, 4)))
:: prints: 1/2

frac_coerce(value)

Coerces a plain integer Number or an existing Fraction into a Fraction.

print(frac_display(frac_coerce(5)))
:: prints: 5

frac_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/6

frac_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/6

frac_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/2

frac_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: 2

frac_negate(f)

Negates a Fraction, flipping the sign of its numerator.

print(frac_display(frac_negate(frac_new(3, 4))))
:: prints: -3/4

frac_reciprocal(f)

Gives the reciprocal of a Fraction, swapping numerator and denominator.

print(frac_display(frac_reciprocal(frac_new(3, 4))))
:: prints: 4/3

frac_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 yes

frac_is_zero(f)

Tests whether a Fraction is exactly zero.

frac_is_zero(frac_new(0, 5))
:: gives yes

frac_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 yes

frac_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 1

frac_to_number(f)

Converts a Fraction to the nearest Number, via true division.

frac_to_number(frac_new(1, 4))
:: gives 0.25

frac_from_text(text)

print(frac_display(frac_from_text("3/4")))
:: prints: 3/4

frac_display(f)

print(frac_display(frac_from_text("5")))
:: prints: 5

Get notified when vfraction changes

Get notified about

New versions and security advisories.

New and updated packages, once a day.

News on the math model built in Venutian.