Rational: exact fractions alongside BigDecimal
メンテナーはふだん 1 日以内に返信
評価
この issue はまだ評価されていません。
説明
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.
- 主要言語
- TypeScript
- スター
- 16.7k
- フォーク
- 808
- 平均マージ
- 11時間 11分
- マージ済み PR(30日)
- 840
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Effect-TS/effect のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
@effect/ai-anthropic: text_editor_20250728 insert decodes `new_str`, but the API sends `insert_text`オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
[email protected]: crypto/KeyFormat declarations require the global JsonWebKey, breaking Node-only type-checks対応中かも @effect-bot が今日担当しました。 オープン
Effect-TS/effect#10115 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 4/5 1〜2日 初心者へのやさしさ 55/100
メンテナーはふだん 1 日以内に返信
-
sql-pg: send several statements in one extended-query Sync (transaction-local context without extra round trips)対応中かも @effect-bot が今日担当しました。 オープン
Effect-TS/effect#10113 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
Effect-TS/effect の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Graphify-Labs/graphify#4860 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
sveltejs/eslint-plugin-svelte#1615 ·
メンテナーはふだん 7 日以内に返信
-
Parallel reticle_verify opens per-flow leases without the source lease's ignoreHTTPSErrors対応中かも @as992949791 が今日担当しました。 オープンgood first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
reticlehq/reticle#2033 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信