-
Notifications
You must be signed in to change notification settings - Fork 206
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
Conversation
Deploying agoric-sdk with Cloudflare Pages
|
348bd7c
to
694bed1
Compare
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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
if (oldDen < newDen) { | |
if (newDen > oldDen) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very correct.
694bed1
to
118e6ad
Compare
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.