From d6982fe977c7403ad1c6188a6b656109091b499c Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Sat, 23 Dec 2023 11:05:48 -0800 Subject: [PATCH 1/2] Update dec.rs --- core/src/types/dec.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/core/src/types/dec.rs b/core/src/types/dec.rs index 3a77b991e8..9dfd99422c 100644 --- a/core/src/types/dec.rs +++ b/core/src/types/dec.rs @@ -52,7 +52,33 @@ pub type Result = std::result::Result; pub struct Dec(pub I256); impl Dec { - /// Division with truncation (TODO: better description) + /// Performs division with truncation. + /// + /// This method divides `self` by `rhs` (right-hand side) and truncates the result to [`POS_DECIMAL_PRECISION`] + /// decimal places. Truncation here means that any fractional part of the result that exceeds + /// [`POS_DECIMAL_PRECISION`] is discarded. + /// + /// The division is performed in the following way: + /// 1. The absolute values of the numerator and denominator are used for the division. + /// 2. The result is calculated to a fixed precision defined by [`POS_DECIMAL_PRECISION`]. + /// 3. If either the numerator or denominator (but not both) is negative, the result is negated. + /// 4. If the division is impossible (e.g., division by zero or overflow), `None` is returned. + /// + /// Example: + /// ``` + /// + /// let x = Dec::new(3, 1).unwrap(); // Represents 0.3 + /// let y = Dec::new(2, 1).unwrap(); // Represents 0.2 + /// let result = x.trunc_div(&y).unwrap(); + /// assert_eq!(result, Dec::new(15, 1).unwrap()); // Result is 1.5 truncated to 1 decimal place + /// ``` + /// + /// # Arguments + /// * `rhs`: The right-hand side `Dec` value for the division. + /// + /// # Returns + /// An `Option` which is `Some` with the result if the division is successful, or `None` if the division + /// cannot be performed. pub fn trunc_div(&self, rhs: &Self) -> Option { let is_neg = self.0.is_negative() ^ rhs.0.is_negative(); let inner_uint = self.0.abs(); From 13a68808adbd039d0e6daebddb87574e9f75276a Mon Sep 17 00:00:00 2001 From: brentstone Date: Wed, 27 Dec 2023 16:58:26 -0700 Subject: [PATCH 2/2] fmt --- core/src/types/dec.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/src/types/dec.rs b/core/src/types/dec.rs index 9dfd99422c..21d3158115 100644 --- a/core/src/types/dec.rs +++ b/core/src/types/dec.rs @@ -54,15 +54,18 @@ pub struct Dec(pub I256); impl Dec { /// Performs division with truncation. /// - /// This method divides `self` by `rhs` (right-hand side) and truncates the result to [`POS_DECIMAL_PRECISION`] - /// decimal places. Truncation here means that any fractional part of the result that exceeds + /// This method divides `self` by `rhs` (right-hand side) and truncates the + /// result to [`POS_DECIMAL_PRECISION`] decimal places. Truncation here + /// means that any fractional part of the result that exceeds /// [`POS_DECIMAL_PRECISION`] is discarded. /// /// The division is performed in the following way: - /// 1. The absolute values of the numerator and denominator are used for the division. - /// 2. The result is calculated to a fixed precision defined by [`POS_DECIMAL_PRECISION`]. - /// 3. If either the numerator or denominator (but not both) is negative, the result is negated. - /// 4. If the division is impossible (e.g., division by zero or overflow), `None` is returned. + /// 1. The absolute values of the numerator and denominator are used for the + /// division. 2. The result is calculated to a fixed precision defined + /// by [`POS_DECIMAL_PRECISION`]. 3. If either the numerator or + /// denominator (but not both) is negative, the result is negated. 4. If + /// the division is impossible (e.g., division by zero or overflow), `None` + /// is returned. /// /// Example: /// ``` @@ -77,8 +80,8 @@ impl Dec { /// * `rhs`: The right-hand side `Dec` value for the division. /// /// # Returns - /// An `Option` which is `Some` with the result if the division is successful, or `None` if the division - /// cannot be performed. + /// An `Option` which is `Some` with the result if the division is + /// successful, or `None` if the division cannot be performed. pub fn trunc_div(&self, rhs: &Self) -> Option { let is_neg = self.0.is_negative() ^ rhs.0.is_negative(); let inner_uint = self.0.abs();