Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Rational: exact fractions alongside BigDecimal

Abierto
#9,979 0 comentarios 1 reacción 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@effect-bot ya está trabajando en esto.

Desde el 26/10/2026.

  • #9990 de @effect-bot — abierto

Evaluación

Este issue todavía no se ha evaluado.

Descripción

What is the problem this feature would solve?

Effect has exact numeric modules for integers (BigInt) and terminating decimals (BigDecimal), but nothing for exact ratios whose decimal expansion does not terminate.

BigDecimal cannot represent 1/3, 25/24 or (6/5)⁻³ exactly; division cuts the result off at a fixed scale:

const third = BigDecimal.divideUnsafe(BigDecimal.fromBigInt(1n), BigDecimal.fromBigInt(3n))
BigDecimal.equals(BigDecimal.multiply(third, BigDecimal.fromBigInt(3n)), BigDecimal.fromBigInt(1n)) // false

So any code that compares derived ratios has to choose between float error and its own fraction type. Typical cases:

  • Musical and typographic scales: intervals such as 6/5 and 5/4 are compounded and compared against a bound, for example to decide which steps of a type scale stay within a limit. A step that lands exactly on the bound must compare as equal, not as one ulp above it.
  • Unit conversion and aspect ratios, kept exact until the final output.
  • Probability and scheduling weights, where sums of fractions must stay exact.

Today each project writes its own small Fraction module (lowest terms, cross-product ordering, a schema), which is exactly the kind of foundational data type the standard library is for.

What is the feature you are proposing to solve the problem?

A Rational module next to BigDecimal, following the same conventions:

import { Rational } from "effect"

const r = Rational.make(6n, 5n)               // always in lowest terms, positive denominator
Rational.fromBigInt(3n)
Rational.fromBigDecimal(BigDecimal.fromStringUnsafe("1.25"))  // 5/4, exact
Rational.fromNumber(0.5)                      // Option: exact for finite floats

Rational.sum / subtract / multiply / pow      // exact
Rational.divide(a, b)                         // Option, none on zero
Rational.Order, Rational.Equivalence          // cross-product ordering, no division
Rational.isLessThanOrEqualTo, min, max, clamp // via Order
Rational.toNumber(r)                          // lossy, explicitly
Rational.toBigDecimal(r, scale)               // lossy at the given scale
Rational.format(r)                            // "6/5"

Schema.Rational                               // codec, e.g. from "6/5"
  • Representation: bigint numerator and denominator, like BigDecimal, so compounding stays exact without overflow.
  • Canonical form: lowest terms with a positive denominator, so structural equality matches numeric equality and Equal/Hash need no normalisation.
What alternatives have you considered?
  • BigDecimal with a large scale: still rounds non-terminating ratios, and equality at the boundary depends on the chosen scale.
  • A local Fraction type per project: works, but repeats the same gcd, ordering and schema code everywhere, and number fields lose exactness above 2⁵³ once cross products grow.
  • Third-party packages (fraction.js, big-rational): they don't integrate with Order, Equal, Schema or the dual/pipe style, and add a dependency for a small, foundational type.
Lenguaje dominante
TypeScript
Estrellas
16.7k
Forks
808
Merge medio
11 h 33 min
PR fusionados (30 d)
855

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Effect-TS/effect

Todos los issues de Effect-TS/effect

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.