-
Notifications
You must be signed in to change notification settings - Fork 151
Refactor U256Ext #4034
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
base: main
Are you sure you want to change the base?
Refactor U256Ext #4034
Conversation
Summary of ChangesHello @squadgazzz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request is a great improvement. It refactors the U256Ext trait to deduplicate implementations and leverages the ruint library to simplify conversions and improve correctness, especially for checked_mul_f64 and checked_ceil_div for alloy::U256. The addition of unit tests is also very welcome.
I've found a few areas for improvement:
- Some of the new implementations using
.then(...).flatten()could be made more readable with simpleifstatements.
Overall, excellent work on this refactoring.
# Conflicts: # Cargo.lock # crates/number/src/u256_ext.rs
| (!other.is_zero()).then(|| self.div_ceil(*other)) | ||
| } | ||
|
|
||
| fn checked_mul_f64(&self, factor: f64) -> Option<Self> { |
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.
I wonder if using BigRational instead of floats would be better/more accurate/less error prone
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.
But on the callee side, we use f64 everywhere. So it would not be really convenient to first convert it to BigRational or anything else.
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.
I meant internally to this function (wasn't very clear, sorry about that)
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.
I assume we can't.
For example, with BigRational::from_float(0.2):
- Returns something like 3602879701896397/18014398509481984 (the exact rational for f64's approximation of 0.2)
- This propagates the imprecision through the calculation
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.
This propagates the imprecision through the calculation
The calculation already has a very specific loss of precision because we use the SCALE factors to scale up and later scale down. I think doing a quick comparison between the current implementation and big rationals would still be good. If big rationals are okay the code would be a lot simpler.
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.
If I use this BigRational implementation:
fn checked_mul_f64_bigrational(value: &U256, factor: f64) -> Option<U256> {
use num::Zero;
if !factor.is_finite() || factor.is_sign_negative() {
return None;
}
if factor.is_zero() {
return Some(U256::ZERO);
}
// Convert f64 to BigRational - this captures the exact binary representation
let factor_rational = BigRational::from_float(factor)?;
// Convert U256 to BigRational
let value_rational = value.to_big_rational();
// Multiply
let result_rational = value_rational * factor_rational;
// Convert back to U256 (truncating)
U256::from_big_rational(&result_rational)
}The following test fails
#[test]
fn bigrational_is_not_decimal_safe_for_0_1() {
// Intentional failing test: demonstrates that the BigRational variant
// does not match decimal intent for a common factor.
let value = U256::from(100_000_000_000_000_000_000u128); // 1e20
let factor_str = "0.1";
let factor = f64::from_str(factor_str).expect("valid factor");
let scaled = value.checked_mul_f64(factor).expect("scaled");
let big = checked_mul_f64_bigrational(&value, factor).expect("bigrational");
assert_eq!(
scaled.to_string(),
big.to_string()
);
}, with the error:
left: "10000000000000000000"
right: "10000000000000000555"
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.
Was not expecting this, than can you leave a comment explaining why we're using using the bigrational approach? Might be useful for future reference
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.
Added a doc
Description
Changes
alloy::U256.div_ceilforalloy::U256.checked_mul_f64.ResulttoOptionfor some functions since errors from results are not used in any case.How to test
New unit tests + existing tests.