Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ratio.quantize() shouldn't increase precision unnecessarily #9926

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

Chris-Hibbert
Copy link
Contributor

Description

Ratios are designed to hold fractions as ratios with arbitrary length denominators. A few operations (multiplication) can cause the denominators to grow rapidly. To allow holders of growing ratios to manage this, we provide a method quantize(), which returns a ratio that approximates the original value, but with a smaller denominator.

The current implementation uses the provided denominator even if it is larger. This doesn't gain anything, and can lose precision unnecessarily

Security Considerations

No security implications.

Scaling Considerations

The quantize() method is used to limit the size of very large BigInts. This improvement only makes that better.

Documentation Considerations

No need for changes. The current doc does say "Make an equivalant ratio with a new denominator", even though the result is only an approximation.

Testing Considerations

Added new tests.

Upgrade Considerations

No hurry to get this included in any particular contract upgrade.

@Chris-Hibbert Chris-Hibbert self-assigned this Aug 19, 2024
Copy link

cloudflare-workers-and-pages bot commented Aug 19, 2024

Deploying agoric-sdk with  Cloudflare Pages  Cloudflare Pages

Latest commit: 118e6ad
Status: ✅  Deploy successful!
Preview URL: https://8fefa710.agoric-sdk.pages.dev
Branch Preview URL: https://cth-ratioquantize.agoric-sdk.pages.dev

View logs

Copy link
Member

@turadg turadg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The potential loss of precision is persuasive. E.g. quantizing 1/3 by 10.

Please update the no longer correct jsdoc:

Make an equivalant ratio with a new denominator
(had a typo too)

In the next push, please squash all commits into one. The test change is necessary for the feat commit alone to pass CI.

@@ -352,6 +352,10 @@ export const ratiosSame = (left, right) => {
export const quantize = (ratio, newDen) => {
const oldDen = ratio.denominator.value;
const oldNum = ratio.numerator.value;
if (oldDen < newDen) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignorable nit: testing the param against the status quo reads more conventionally to me

Suggested change
if (oldDen < newDen) {
if (newDen > oldDen) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very correct.

@Chris-Hibbert Chris-Hibbert added the automerge:rebase Automatically rebase updates, then merge label Aug 20, 2024
@mergify mergify bot merged commit 9986152 into master Aug 20, 2024
90 checks passed
@mergify mergify bot deleted the cth-ratioQuantize branch August 20, 2024 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge:rebase Automatically rebase updates, then merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants