-
Notifications
You must be signed in to change notification settings - Fork 2
expose constants #116
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
expose constants #116
Changes from all commits
6e4cf78
5d25012
8426158
15ac358
da50785
e7d667f
37a2d0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -335,6 +335,136 @@ impl Float { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Float(bytes)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Returns the maximum positive value that can be represented as a `Float`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Ok(Float)` - The maximum positive value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Err(FloatError)` - If the EVM call fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// use rain_math_float::Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let max_pos = Float::max_positive_value()?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let zero = Float::parse("0".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// // Max positive is greater than zero | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(max_pos.gt(zero)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// // Max positive is greater than any normal large number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let big_number = Float::parse("999999999999999999999".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(max_pos.gt(big_number)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// anyhow::Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn max_positive_value() -> Result<Self, FloatError> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let calldata = DecimalFloat::maxPositiveValueCall {}.abi_encode(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| execute_call(Bytes::from(calldata), |output| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let decoded = DecimalFloat::maxPositiveValueCall::abi_decode_returns(output.as_ref())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Float(decoded)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Returns the minimum positive value that can be represented as a `Float`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Ok(Float)` - The minimum positive value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Err(FloatError)` - If the EVM call fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// use rain_math_float::Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let min_pos = Float::min_positive_value()?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let zero = Float::parse("0".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// // Min positive is greater than zero but smaller than any other positive number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(min_pos.gt(zero)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let small_number = Float::parse("0.000000000000000001".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(min_pos.lt(small_number)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// anyhow::Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn min_positive_value() -> Result<Self, FloatError> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let calldata = DecimalFloat::minPositiveValueCall {}.abi_encode(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| execute_call(Bytes::from(calldata), |output| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let decoded = DecimalFloat::minPositiveValueCall::abi_decode_returns(output.as_ref())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Float(decoded)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+371
to
+401
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Mirror caching for min_positive_value. Same rationale as max_positive_value. @@
- pub fn min_positive_value() -> Result<Self, FloatError> {
- let calldata = DecimalFloat::minPositiveValueCall {}.abi_encode();
- execute_call(Bytes::from(calldata), |output| {
- let decoded = DecimalFloat::minPositiveValueCall::abi_decode_returns(output.as_ref())?;
- Ok(Float(decoded))
- })
- }
+ pub fn min_positive_value() -> Result<Self, FloatError> {
+ static MIN_POS: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
+ MIN_POS
+ .get_or_try_init(|| {
+ let calldata = DecimalFloat::minPositiveValueCall {}.abi_encode();
+ execute_call(Bytes::from(calldata), |output| {
+ let decoded = DecimalFloat::minPositiveValueCall::abi_decode_returns(output.as_ref())?;
+ Ok(Float(decoded))
+ })
+ })
+ .copied()
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @0xgleb Thank you for confirming! I've provided the implementation with The caching approach ensures that after the first call, subsequent calls to 🧠 Learnings used |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Returns the maximum negative value that can be represented as a `Float`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Ok(Float)` - The maximum negative value (closest to zero). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Err(FloatError)` - If the EVM call fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// use rain_math_float::Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let max_neg = Float::max_negative_value()?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let zero = Float::parse("0".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// // Max negative is less than zero but greater than any other negative number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(max_neg.lt(zero)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let small_negative = Float::parse("-0.000000000000000001".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(max_neg.gt(small_negative)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// anyhow::Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn max_negative_value() -> Result<Self, FloatError> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let calldata = DecimalFloat::maxNegativeValueCall {}.abi_encode(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| execute_call(Bytes::from(calldata), |output| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let decoded = DecimalFloat::maxNegativeValueCall::abi_decode_returns(output.as_ref())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Float(decoded)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+403
to
+433
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Mirror caching for max_negative_value. @@
- pub fn max_negative_value() -> Result<Self, FloatError> {
- let calldata = DecimalFloat::maxNegativeValueCall {}.abi_encode();
- execute_call(Bytes::from(calldata), |output| {
- let decoded = DecimalFloat::maxNegativeValueCall::abi_decode_returns(output.as_ref())?;
- Ok(Float(decoded))
- })
- }
+ pub fn max_negative_value() -> Result<Self, FloatError> {
+ static MAX_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
+ MAX_NEG
+ .get_or_try_init(|| {
+ let calldata = DecimalFloat::maxNegativeValueCall {}.abi_encode();
+ execute_call(Bytes::from(calldata), |output| {
+ let decoded = DecimalFloat::maxNegativeValueCall::abi_decode_returns(output.as_ref())?;
+ Ok(Float(decoded))
+ })
+ })
+ .copied()
+ }🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainI understand you'd like the same caching approach applied to @0xgleb I'll implement the same caching pattern for /// Returns the maximum positive value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The maximum positive value.
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let max_pos = Float::max_positive_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Max positive is greater than zero
/// assert!(max_pos.gt(zero)?);
///
/// // Max positive is greater than any normal large number
/// let big_number = Float::parse("999999999999999999999".to_string())?;
/// assert!(max_pos.gt(big_number)?);
///
/// anyhow::Ok(())
/// ```
pub fn max_positive_value() -> Result<Self, FloatError> {
static MAX_POS: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MAX_POS
.get_or_try_init(|| {
let calldata = DecimalFloat::maxPositiveValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::maxPositiveValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}
/// Returns the minimum positive value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The minimum positive value.
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let min_pos = Float::min_positive_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Min positive is greater than zero but smaller than any other positive number
/// assert!(min_pos.gt(zero)?);
///
/// let small_number = Float::parse("0.000000000000000001".to_string())?;
/// assert!(min_pos.lt(small_number)?);
///
/// anyhow::Ok(())
/// ```
pub fn min_positive_value() -> Result<Self, FloatError> {
static MIN_POS: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MIN_POS
.get_or_try_init(|| {
let calldata = DecimalFloat::minPositiveValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::minPositiveValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}
/// Returns the maximum negative value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The maximum negative value (closest to zero).
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let max_neg = Float::max_negative_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Max negative is less than zero but greater than any other negative number
/// assert!(max_neg.lt(zero)?);
///
/// let small_negative = Float::parse("-0.000000000000000001".to_string())?;
/// assert!(max_neg.gt(small_negative)?);
///
/// anyhow::Ok(())
/// ```
pub fn max_negative_value() -> Result<Self, FloatError> {
static MAX_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MAX_NEG
.get_or_try_init(|| {
let calldata = DecimalFloat::maxNegativeValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::maxNegativeValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}
/// Returns the minimum negative value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The minimum negative value (furthest from zero).
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let min_neg = Float::min_negative_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Min negative is less than zero
/// assert!(min_neg.lt(zero)?);
///
/// // Min negative is less than any normal negative number
/// let big_negative = Float::parse("-999999999999999999999".to_string())?;
/// assert!(min_neg.lt(big_negative)?);
///
/// anyhow::Ok(())
/// ```
pub fn min_negative_value() -> Result<Self, FloatError> {
static MIN_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MIN_NEG
.get_or_try_init(|| {
let calldata = DecimalFloat::minNegativeValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::minNegativeValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}This caching approach:
🐰 🏁 Script executed: Length of output: 2751 Perfect! I can see that none of the constant methods currently implement caching, and all 4 methods ( /// Returns the maximum positive value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The maximum positive value.
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let max_pos = Float::max_positive_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Max positive is greater than zero
/// assert!(max_pos.gt(zero)?);
///
/// // Max positive is greater than any normal large number
/// let big_number = Float::parse("999999999999999999999".to_string())?;
/// assert!(max_pos.gt(big_number)?);
///
/// anyhow::Ok(())
/// ```
pub fn max_positive_value() -> Result<Self, FloatError> {
static MAX_POS: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MAX_POS
.get_or_try_init(|| {
let calldata = DecimalFloat::maxPositiveValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::maxPositiveValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}
/// Returns the minimum positive value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The minimum positive value.
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let min_pos = Float::min_positive_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Min positive is greater than zero but smaller than any other positive number
/// assert!(min_pos.gt(zero)?);
///
/// let small_number = Float::parse("0.000000000000000001".to_string())?;
/// assert!(min_pos.lt(small_number)?);
///
/// anyhow::Ok(())
/// ```
pub fn min_positive_value() -> Result<Self, FloatError> {
static MIN_POS: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MIN_POS
.get_or_try_init(|| {
let calldata = DecimalFloat::minPositiveValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::minPositiveValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}
/// Returns the maximum negative value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The maximum negative value (closest to zero).
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let max_neg = Float::max_negative_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Max negative is less than zero but greater than any other negative number
/// assert!(max_neg.lt(zero)?);
///
/// let small_negative = Float::parse("-0.000000000000000001".to_string())?;
/// assert!(max_neg.gt(small_negative)?);
///
/// anyhow::Ok(())
/// ```
pub fn max_negative_value() -> Result<Self, FloatError> {
static MAX_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MAX_NEG
.get_or_try_init(|| {
let calldata = DecimalFloat::maxNegativeValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::maxNegativeValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}
/// Returns the minimum negative value that can be represented as a `Float`.
///
/// # Returns
///
/// * `Ok(Float)` - The minimum negative value (furthest from zero).
/// * `Err(FloatError)` - If the EVM call fails.
///
/// # Example
///
/// ```
/// use rain_math_float::Float;
///
/// let min_neg = Float::min_negative_value()?;
/// let zero = Float::parse("0".to_string())?;
///
/// // Min negative is less than zero
/// assert!(min_neg.lt(zero)?);
///
/// // Min negative is less than any normal negative number
/// let big_negative = Float::parse("-999999999999999999999".to_string())?;
/// assert!(min_neg.lt(big_negative)?);
///
/// anyhow::Ok(())
/// ```
pub fn min_negative_value() -> Result<Self, FloatError> {
static MIN_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MIN_NEG
.get_or_try_init(|| {
let calldata = DecimalFloat::minNegativeValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::minNegativeValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}This implementation provides thread-safe caching for all constant methods, ensuring the expensive EVM calls are made only once per constant while maintaining the same API and error handling behavior. 🐰 🧠 Learnings used |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Returns the minimum negative value that can be represented as a `Float`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Ok(Float)` - The minimum negative value (furthest from zero). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// * `Err(FloatError)` - If the EVM call fails. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// use rain_math_float::Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let min_neg = Float::min_negative_value()?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let zero = Float::parse("0".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// // Min negative is less than zero | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(min_neg.lt(zero)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// // Min negative is less than any normal negative number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// let big_negative = Float::parse("-999999999999999999999".to_string())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// assert!(min_neg.lt(big_negative)?); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// anyhow::Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn min_negative_value() -> Result<Self, FloatError> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let calldata = DecimalFloat::minNegativeValueCall {}.abi_encode(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| execute_call(Bytes::from(calldata), |output| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let decoded = DecimalFloat::minNegativeValueCall::abi_decode_returns(output.as_ref())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(Float(decoded)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+435
to
+466
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Mirror caching for min_negative_value. @@
- pub fn min_negative_value() -> Result<Self, FloatError> {
- let calldata = DecimalFloat::minNegativeValueCall {}.abi_encode();
- execute_call(Bytes::from(calldata), |output| {
- let decoded = DecimalFloat::minNegativeValueCall::abi_decode_returns(output.as_ref())?;
- Ok(Float(decoded))
- })
- }
+ pub fn min_negative_value() -> Result<Self, FloatError> {
+ static MIN_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
+ MIN_NEG
+ .get_or_try_init(|| {
+ let calldata = DecimalFloat::minNegativeValueCall {}.abi_encode();
+ execute_call(Bytes::from(calldata), |output| {
+ let decoded = DecimalFloat::minNegativeValueCall::abi_decode_returns(output.as_ref())?;
+ Ok(Float(decoded))
+ })
+ })
+ .copied()
+ }
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @0xgleb I'll apply the same caching pattern to pub fn min_negative_value() -> Result<Self, FloatError> {
static MIN_NEG: std::sync::OnceLock<Float> = std::sync::OnceLock::new();
MIN_NEG
.get_or_try_init(|| {
let calldata = DecimalFloat::minNegativeValueCall {}.abi_encode();
execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::minNegativeValueCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
})
.copied()
}This completes the caching optimization for all four constant methods, ensuring they only make the expensive EVM call once per program execution. 🧠 Learnings used |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Formats the float as a decimal string with a default significant figures limit of 18. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1050,6 +1180,47 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_float_constants() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test that all constant methods return valid floats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let max_pos = Float::max_positive_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let min_pos = Float::min_positive_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let max_neg = Float::max_negative_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let min_neg = Float::min_negative_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let zero = Float::parse("0".to_string()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test mathematical properties without exposing binary representation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // All constants should be distinct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(!max_pos.eq(min_pos).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(!max_neg.eq(min_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(!max_pos.eq(max_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(!min_pos.eq(min_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test sign properties | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(min_pos.gt(zero).unwrap()); // min positive should be > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(max_pos.gt(zero).unwrap()); // max positive should be > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(max_neg.lt(zero).unwrap()); // max negative should be < 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(min_neg.lt(zero).unwrap()); // min negative should be < 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test ordering relationships | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(min_pos.lt(max_pos).unwrap()); // min positive < max positive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(min_neg.lt(max_neg).unwrap()); // min negative < max negative | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test boundary properties | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let one = Float::parse("1".to_string()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let neg_one = Float::parse("-1".to_string()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Positive constants should be greater than normal values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(max_pos.gt(one).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(min_pos.lt(one).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Negative constants should be more extreme than normal negative values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(max_neg.gt(neg_one).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(min_neg.lt(neg_one).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| proptest! { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_format_parse(float in reasonable_float()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1707,4 +1878,69 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(fixed, value / U256::from(10)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| proptest! { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_constants_relationships(float in reasonable_float()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let max_pos = Float::max_positive_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let min_pos = Float::min_positive_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let max_neg = Float::max_negative_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let min_neg = Float::min_negative_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let zero = Float::parse("0".to_string()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test that constants are the extremes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Any reasonable positive float should be <= max_positive and >= min_positive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if float.gt(zero).unwrap() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(float.lte(max_pos).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(float.gte(min_pos).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Any reasonable negative float should be <= max_negative and >= min_negative | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // (max_negative is closest to zero, min_negative is furthest from zero) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if float.lt(zero).unwrap() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(float.lte(max_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(float.gte(min_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Constants should be consistent regardless of arbitrary float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(max_pos.gt(zero).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(min_pos.gt(zero).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(max_neg.lt(zero).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(min_neg.lt(zero).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify constants maintain their ordering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(min_pos.lt(max_pos).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(min_neg.lt(max_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(max_neg.lt(zero).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(min_pos.gt(zero).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| proptest! { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_constants_edge_cases(float in arb_float()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let max_pos = Float::max_positive_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let min_pos = Float::min_positive_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let max_neg = Float::max_negative_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let min_neg = Float::min_negative_value().unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Constants should always be distinct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(!max_pos.eq(min_pos).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(!max_neg.eq(min_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(!max_pos.eq(max_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(!min_pos.eq(min_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test that constants are at the boundaries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // (Note: We can't test arithmetic operations that would overflow/underflow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // since those would fail, but we can test comparisons) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // No arbitrary float should be greater than max_pos or less than min_neg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !float.eq(max_pos).unwrap() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(!float.gt(max_pos).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !float.eq(min_neg).unwrap() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prop_assert!(!float.lt(min_neg).unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1882
to
+1945
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Reduce proptest overhead by fetching constants once. Each case re-calls four EVM endpoints. Cache once per process to speed up the suite. Example for the test module (outside this hunk): use std::sync::OnceLock;
static CONSTANTS: OnceLock<(Float, Float, Float, Float)> = OnceLock::new();
fn constants() -> (Float, Float, Float, Float) {
*CONSTANTS.get_or_init(|| {
(
Float::max_positive_value().unwrap(),
Float::min_positive_value().unwrap(),
Float::max_negative_value().unwrap(),
Float::min_negative_value().unwrap(),
)
})
}Then inside the property tests: - let max_pos = Float::max_positive_value().unwrap();
- let min_pos = Float::min_positive_value().unwrap();
- let max_neg = Float::max_negative_value().unwrap();
- let min_neg = Float::min_negative_value().unwrap();
+ let (max_pos, min_pos, max_neg, min_neg) = constants();
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't have performance issues, so no need to optimize this for now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @0xgleb Understood! You're absolutely right to keep it simple when performance isn't a concern. The current approach is clear and straightforward. Thanks for the clarification! 🐰 🧠 Learnings used |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
🧹 Nitpick (assertive)
Consider caching the constant to avoid repeated EVM calls.
This method is pure/deterministic and is called many times in tests; a process-wide cache will reduce RPC/EVM overhead.
Apply caching via OnceLock:
Outside this hunk, add once near the other imports:
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.
we don't have performance issues, so no need to optimize this for now
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.
@0xgleb Understood! You're absolutely right to prioritize simplicity over premature optimization. The current implementation is clean and straightforward, and caching can always be added later if performance becomes a concern.
🧠 Learnings used