Rational: exact fractions alongside BigDecimal
I maintainer di solito rispondono entro 1 giorno
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
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:
bigintnumerator and denominator, likeBigDecimal, so compounding stays exact without overflow. - Canonical form: lowest terms with a positive denominator, so structural equality matches numeric equality and
Equal/Hashneed no normalisation.
What alternatives have you considered?
BigDecimalwith a large scale: still rounds non-terminating ratios, and equality at the boundary depends on the chosen scale.- A local
Fractiontype per project: works, but repeats the same gcd, ordering and schema code everywhere, andnumberfields lose exactness above 2⁵³ once cross products grow. - Third-party packages (
fraction.js,big-rational): they don't integrate withOrder,Equal,Schemaor thedual/pipe style, and add a dependency for a small, foundational type.
- Lingua principale
- TypeScript
- Stelle
- 16.7k
- Fork
- 808
- Merge medio
- 11h 33m
- PR unite (30g)
- 855
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Effect-TS/effect
-
`fromJsonSchemaDocument`: import open `patternProperties`Forse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#10048 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
`fromJsonSchemaDocument`: intersect an object with `anyOf` branches that re-declare an enum propertyAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
I maintainer di solito rispondono entro 1 giorno
-
`fromJsonSchemaDocument`: import `dependentRequired` (draft-07 `dependencies`)Forse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#10046 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
`fromJsonSchemaDocument`: import `if` / `then` / `else`Forse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#10045 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
`fromJsonSchemaDocument`: import the `not` keywordForse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#10044 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Effect-TS/effect
Issue simili
-
[Bug]: Pen taps on sidebar rows start a drag and lose their clickForse già presa @makhnatkin l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
b310-digital/teammapper#1515 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
puckeditor/puck#1884 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
outline/outline#14378 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
nicobailon/pi-web-access#642 ·
I maintainer di solito rispondono entro 1 giorno