Conversation
WalkthroughThe changes restructure the SD29x9 and UD30x9 fixed-point type modules by consolidating implementations from nested files into their parent modules. This eliminates the nested file hierarchy while maintaining public APIs and adding new constructors and conversion helpers. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #134 +/- ##
===========================================
+ Coverage 74.82% 96.66% +21.83%
===========================================
Files 13 16 +3
Lines 433 1440 +1007
Branches 110 366 +256
===========================================
+ Hits 324 1392 +1068
+ Misses 106 27 -79
- Partials 3 21 +18
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
math/fixed_point/sources/ud30x9.move (1)
60-71: Document overflow behavior for near-maximum values.The
ceilfunction will abort on overflow whenxhas a non-zero fractional part and its integer component is close toMAX_VALUE. For example,ceil(max())will overflow because the integer part plusSCALEexceedsu128::MAX.This is mathematically correct (the ceiling cannot be represented), but consider documenting this edge case in the function comment or providing a
try_ceilvariant that returnsOption<UD30x9>.
🧹 Nitpick comments (3)
math/fixed_point/sources/sd29x9.move (3)
72-77: Consider documenting abort behavior forabs(min()).When called with
min()(representing -2^127),abswill abort because |−2^127| = 2^127 exceedsMAX_POSITIVE_VALUE(2^127 − 1). This is mathematically correct behavior for signed integers, but documenting this edge case would help users avoid unexpected aborts.
154-159: Redundant mask inlshift.The
& U128_MAX_VALUEmask on line 158 is redundant because the left shift on au128already produces au128result with upper bits naturally discarded.Suggested simplification
public fun lshift(x: SD29x9, bits: u8): SD29x9 { if (bits >= 128) { return zero() }; - from_bits((x.unwrap() << bits) & U128_MAX_VALUE) + from_bits(x.unwrap() << bits) }
181-187: Missing division-by-zero check inmod.The PR description mentions wanting to add a custom abort for
mod. Currently, ifyis zero, line 185 will abort with Move's default division-by-zero error rather than a customEOverflowor dedicated error constant.If a custom error is desired for consistency with other operations, consider adding an explicit check:
Suggested implementation with explicit error
+#[error(code = 1)] +const EDivisionByZero: vector<u8> = b"Division by zero"; + public fun mod(x: SD29x9, y: SD29x9): SD29x9 { let x_components = decompose(x.unwrap()); let y_components = decompose(y.unwrap()); + if (y_components.mag == 0) { + abort EDivisionByZero + }; let remainder = x_components.mag % y_components.mag; wrap_components(Components { neg: x_components.neg, mag: remainder }) }
ericnordelo
left a comment
There was a problem hiding this comment.
I needed to declare additional error constants in the sd29x9_base module, but an EOverflow error was already declared in the sd29x9 file. On top of that, aborting with EOverflow from sd29x9_base is problematic, since constants cannot be imported in Move (and making a separate abort function is weird).
The whole idea of the design of the two modules is to make the type easier to read and understand for users, making the library as readable as possible. I understand that module system in Sui is hard to deal with, but while I see the issue with redeclaring a similar error in two modules, I think that's not worst than putting everything in a single big file, making it harder to read for our users.
I vote we keep it, and bring the discussion to the Sui team to see if they have concerns with any of the approaches or strong opinions.
|
IMO splitting the type into 2 separate files and duplicating the constants and error codes makes it more difficult to reason about a type, not easier. In the initial design with 2 files most of the code of the |
Is not 20 extra lines, is just the 20 lines the user need to look to understand the type and the features, he only need to go to the other file to see each function implementation, which is usually not what users want. In this context, the alternative to removing the twenty lines is making users have to go through a few hundred/thousands lines long file if they want ot see what is avaiable in the type. I really don't think that's worth removing a few errors. If there are other strong reasons like maybe error discoverability/tracking in the UI, maybe then is worth considering making the library harder to read, and that's why I suggest bringing Sui into the discussion. But I don't feel there should be that strong issues with errors justifying losing readability. |
|
@immrsd can we close that PR? |
|
IMHO we should close this PR for now. |
I ran into an issue while implementing the
divfunction (and adding an abort with a custom error formod). I needed to declare additional error constants in thesd29x9_basemodule, but anEOverflowerror was already declared in thesd29x9file. On top of that, aborting withEOverflowfromsd29x9_baseis problematic, since constants cannot be imported in Move (and making a separate abort function is weird).I experimented with several approaches, starting with moving most of the implementation logic into
sd29x9_base(including thewrapfunction). However, this led to issues when constructingSD29x9values from a module that does not declare the type.After some consideration, I came to the conclusion that the best approach is to declare a fixed-point type and its math functions in the same file and restructured the package accordingly.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.